I am trying to get my datagridview to display more than one row of data.
Every time I call the program it only writes the 1st line of data in the datagridview instead of adding it to a new row.
I have been looking on the forums for hours but combined with my inexperience I can not understand it.
The following code is in a loop and it reads an xml file each time. The goal is to put the xml data from each file into a new row in the table.
Public Sub ReadData(ByVal filename As String, ByVal file As String)
Try
DS.ReadXml(filename)
DS.Tables.Add("MyTable")
With DS.Tables("MyTable")
.Columns.Add("Title 1")
.Columns.Add("Title 2")
.Columns.Add("Title 3")
.Columns.Add("Title 4")
.Columns.Add("Title 5")
.Columns.Add("Title 6")
End With
Using reader = New StreamReader(filename)
Dim line As String = reader.ReadToEnd()
rtb_Subsidary.AppendText(file & vbCrLf)
DS.Tables("MyTable").Rows.Add(file, "test 1", "test 2", "test 3", "test 4", "test 5", "test 6")
End Using
With dgv_Lic
.DataSource = DS.Tables("MyTable")
.ReadOnly = True
.ScrollBars = ScrollBars.Vertical
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
.AutoResizeColumns()
.RowHeadersVisible = False
For Each col As DataGridViewColumn In .Columns
col.SortMode = DataGridViewColumnSortMode.NotSortable
Next
End With
...end code
It looks like the logic flow of your xml reading isn’t quite right. Also the reading of the xml doesn’t look correct. The rest of what you are doing looks ok but the order you have your statements in (and the xml reading) in means they don’t end up doing quite what you want.
My reading of the steps of your current code is:
My VB.Net is very rusty so rather than giving you some code that won’t work (or some c#) here is a piece of pseudo code that should see you on the right way.
So, in very poor VB like pseudo code:
As an aside – for this sort of problem a debugger (as you have in Visual Studio) can be great, it allows you to step through the execution of your application line by line seeing exactly what is going on.