开发者

vb.net 2010 datagridview issues - data only populates the first row

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 foll开发者_C百科owing 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:

  1. For each xml file call read data
  2. Create or at least re-initialize the dataset
  3. Read your xml into a string variable (but you don't do anything with it)
  4. Add a single row to the dataset (it isn't clear what rtb_Subsidary.AppendText(file & vbCrLf) does)
  5. Overwrite the datasource of the DataGridView with the newly initialized DataTable (which due to the back xml reading only has one line

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.

  1. Do not initialize your table in the read data method - create the table elsewhere and also assign it as the DataGridView datasource elsewhere
  2. In read data in the readdata method in to a temp table and then use DataTable.Merge to combine this new data with your old data.

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜