|
-
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!
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
|