-
Jul 11th, 2013, 04:57 PM
#1
Thread Starter
Lively Member
Populating a DataGridView with different value types?
[Using VB 2010 / Winforms / targeting .NET 2.0]
Hey guys,
Been working on a project that contains a DataGridView, and just got a bit hung up on part of it. The DGV is unbound and is not connected to any database or anything... I am simply populating it on form load by using StreamReader to grab the data from a local CSV file. Like so...
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Read data from CSV file and populate DGV with it
Dim fileName As String = Application.StartupPath & "\Data.csv"
Dim TextLine As String = ""
Dim SplitLine() As String
If System.IO.File.Exists(fileName) = True Then
Dim objReader As New System.IO.StreamReader(fileName)
Do While objReader.Peek() <> -1
TextLine = objReader.ReadLine()
SplitLine = Split(TextLine, ",")
DataGridView1.Rows.Add(SplitLine)
Loop
objReader.Dispose()
Else
MsgBox("CSV file not found.")
End If
End Sub
The DGV has 3 columns. Let's say they are named "FoodName", "FoodType", and "FoodImage". The "FoodName" and "FoodType" columns will hold text data, and the "FoodImage" column will hold images (these images will be stored in My.Resources).
So, the contents of the CSV file might look something like this...
Apple, Fruit, AppleImage
Broccoli, Veggie, BroccoliImage
Cantaloup, Fruit, CantaloupImage
So what I can't figure out is .... when the rows are being added to the DGV during form load... when it gets to a cell that is (ultimately) supposed to hold an image, I need it to change that cell's value type from "text" to "image", and then populate that cell with the correct image from My.Resources.
Here's how I've tried to accomplish that during the DGV "rows added" event (which fires as each row is being added as the stream reader is reading the CSV file). FYI, I know that the commented lines below don't work, but it should give you an ideas as to what I'm trying to do...
Code:
Private Sub DataGridView1_RowsAdded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
For Each Row As DataGridViewRow In DataGridView1.Rows
For Each Cell As DataGridViewCell In Row.Cells
If Cell.Value = "AppleImage" Then
Cell.ValueType = Image ' Change the cell value type from 'Text' to 'Image'
Cell.Value = My.Resources.AppleImage ' Populate the cell with the correct image
ElseIf Cell.Value = "BroccoliImage" Then
Cell.ValueType = Image ' Change the cell value type from 'Text' to 'Image'
Cell.Value = My.Resources.BroccoliImage ' Populate the cell with the correct image
ElseIf Cell.Value = "CantaloupImage" Then
Cell.ValueType = Image ' Change the cell value type from 'Text' to 'Image'
Cell.Value = My.Resources.CantaloupImage ' Populate the cell with the correct image
End If
Next
Next
End Sub
FYI, the user will be able to add new data to the cells in the DGV. Then, on form close, the data from the DGV will be written to the CSV file (so I will need to convert each "image" cell back to its "text" representation so that I can store it all in the CSV file). Here's the sub I'm using to do that, which I call during the 'form closing' event...
Code:
Public Sub SaveDataToFile(ByRef fileName As String)
Dim I As Integer = 0
Dim j As Integer = 0
Dim cellvalue$
Dim rowLine As String = ""
Try
Dim objWriter As New System.IO.StreamWriter(fileName, False)
For j = 0 To (DataGridView1.Rows.Count - 1)
For I = 0 To (DataGridView1.Columns.Count - 1)
If Not TypeOf DataGridView1.CurrentRow.Cells.Item(I).Value Is DBNull Then
If Not TypeOf DataGridView1.CurrentRow.Cells.Item(I).Value Is Image Then
cellvalue = DataGridView1.Item(I, j).Value
Else
cellvalue = DataGridView1.Item(I, j).Tag
End If
Else
cellvalue = ""
End If
rowLine = rowLine + cellvalue + ","
Next
objWriter.WriteLine(rowLine)
rowLine = ""
Next
objWriter.Close()
objWriter.Dispose()
Application.DoEvents()
Catch e As Exception
MessageBox.Show("An error occured while writing to the CSV file." + e.ToString())
Finally
FileClose(1)
End Try
End Sub
Hopefully what I'm trying to do makes sense. The code that writes to the CSV file ("SaveDataToFile" sub) seems to work fine. But I just can't figure out how to get the "DataGridView1_RowsAdded" sub to work (specifically, how to convert the text representation of an image to the actual image within the cell). Can anyone tell me what I need to change to make this work? Or am I way off base with my code, and need to do something entirely different?
Sorry for the length of this post, but it's as short as I could make it to explain everything correctly.
Thanks!
-
Jul 11th, 2013, 05:49 PM
#2
Re: Populating a DataGridView with different value types?
when it gets to a cell that is (ultimately) supposed to hold an image, I need it to change that cell's value type from "text" to "image", and then populate that cell with the correct image from My.Resources.
Or you could just add a DataGridViewImageColumn for which the cell.Value property is always of the type Image.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Jul 11th, 2013, 06:04 PM
#3
Thread Starter
Lively Member
Re: Populating a DataGridView with different value types?
Originally Posted by dunfiddlin
Or you could just add a DataGridViewImageColumn for which the cell.Value property is always of the type Image.
I already tried that, but I get an exception when I do that ("invalid cast from 'System.String' to 'System.Drawing.Image"), because the StreamReader in the form load code is populating the DGV one row at a time.
In other words, I don't know how to populate a DGV programmatically (from a CSV file) if each DGV column is a different value type.
Do you know how to do this? I've searched online for a solution for this for a few hours, but haven't found one yet. I'd greatly appreciate a quick example, if anyone knows how this is done!
Last edited by BMAN645; Jul 11th, 2013 at 07:47 PM.
-
Jul 11th, 2013, 08:18 PM
#4
Re: Populating a DataGridView with different value types?
It's simple enough. Instead of just whacking everything in with .... DataGridView1.Rows.Add(SplitLine) ... add the cell values one at a time on a for loop and when the column with the image comes up do your 'if string is so and so thiscell.Value = therightimage' there and then. If you leave it til after the row is complete you're well and truly scuppered!
I would do you it in code but it's 2.15am, I've just got back from walking the dogs, and bed seems like an awfully good idea! Must ... stop ... being distracted by forum!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|