Reputation: 179
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
Upvotes: 0
Views: 2492
Reputation: 33183
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:
Public Class Form1
// A private class level variable of type datatable
Private dt As New DataTable
// You forms constructor
Public Sub New()
InitializeComponent()
// You may be able to get away with this initialization by using it automatically from the xml only once
With td
.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
// Also have you DataGridView code here in the initialization section
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 Sub
// And this is your ReadData method called for each file
Public Sub ReadData(ByVal filename As String)
// Local datatable
Dim dt_temp As New DataTable
Using reader = New StreamReader(filename)
dt_temp.ReadXml(reader)
End Using
// Now merge dt with
dt.Merge(dt_temp)
End Sub
End Class
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.
Upvotes: 1
Reputation: 179
I got it working - I was trying an overly complicated way.
I cant really post an answer because the code changed to much from what I posted above - but thank you for taking the time to reply - it helped big time.
Upvotes: 0