Loading Data Into Correct Data Grid View Columns

  • Thread starter Thread starter JMW.62
  • Start date Start date
J

JMW.62

Guest
Hi Folks,

I am loading data from a Treeview (i.e. user clicks node) and a TextBox (i.e. user copies text using button click) and auto-generating number column and Date and Time column data (See Image). This data is passed to a Listview which can be altered by the user. Then, the user saves the data from Listview to a text file. Then, the data is loaded from the text file and displayed in a DataGridView so the user can manipulate the data (i.e. Filtering, etc.). However, as you can see from the image, the first 8 rows in the DGV load correctly without inserting a text Snippet in the Snippet column. But, when I load the data with a Snippet, the Date and Time data is added to a new/separate row underneath the added row. I have been trying for many hours searching on Google and debugging, etc., with no success? I include (what I think are) relevant code snippets here and hope someone can help me to resolve this problem? I am grateful for any help! :-) Until I can upload an image to show the problem, the DGV columns are in the following order: Number Column = 0 (#); Mapping Column = 1; Snippet Column = 2; Date and Time column = 3.

'DGV SETUP:

'Create an unbound DataGridView by declaring a column count.
DataGridView7.ColumnCount = 4
DataGridView7.ColumnHeadersVisible = True

' Set the column header style.
Dim columnHeaderStyle As New DataGridViewCellStyle()

columnHeaderStyle.BackColor = Color.Beige
columnHeaderStyle.Font = New Font("Verdana", 10, FontStyle.Bold)
DataGridView7.ColumnHeadersDefaultCellStyle = columnHeaderStyle

'Creates columns and sets column header names.
DataGridView7.Columns(0).Name = "#"
DataGridView7.Columns(1).Name = "Mapping Items"
DataGridView7.Columns(2).Name = "Snippet"
DataGridView7.Columns(3).Name = "Date and Time"

With DataGridView7
' .AutoResizeColumnHeadersHeight
.Columns("#").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader
.Columns("Mapping Items").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
.Columns("Snippet").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
.Columns("Date and Time").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End With

'SAVE LISTVIEW TO A SPECIFIC FILE:

' Creates a <CSV> File
Using csv As New System.IO.StreamWriter("C:\Users\CurrentUser\Documents\MappingData.txt", True) 'What does True do here???
For Each Item As ListViewItem In ListView1.Items
csv.WriteLine(String.Format("{0};{1};{2};{3}",
Item.SubItems(0).Text,
Item.SubItems(1).Text,
Item.SubItems(2).Text,
Item.SubItems(3).Text))
Next
End Using

'FORM LOAD EVENT: LOAD MAPPING DGV FROM TEXT FILE:

Private Sub LOADDGV()

Using stream As System.IO.FileStream = System.IO.File.OpenRead("C:\Users\CurrentUser\Documents\MappingData.txt")
Using reader As New System.IO.StreamReader(stream)

Dim line As String = reader.ReadLine()

While (line IsNot Nothing)

Dim columns = line.Split(";") 'Text separator which splits to display in DGV columns (i.e. ;:," ", etc.)
line = reader.ReadLine()

Dim index = DataGridView7.Rows.Add(line)

DataGridView7.Rows(index).SetValues(columns)

End While

End Using
End Using

End Sub




PS: Waiting for my account to be verified to upload an <image>!

Continue reading...
 
Back
Top