datagridview issues - data only populates the first row

Gopher2011

Well-known member
Joined
Mar 3, 2011
Messages
111
Location
South England
Programming Experience
10+
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.



VB.NET:
[COLOR=#00008B]Public[/COLOR][COLOR=#00008B]Sub[/COLOR][COLOR=#000000] ReadData[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] filename [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] file [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]Try[/COLOR][COLOR=#000000]

 DS[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]ReadXml[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]filename[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]

 DS[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Tables[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"MyTable"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]With[/COLOR][COLOR=#000000] DS[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Tables[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"MyTable"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Columns[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"Title 1"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Columns[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"Title 2"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Columns[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"Title 3"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Columns[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"Title 4"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Columns[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"Title 5"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Columns[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"Title 6"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]With[/COLOR][COLOR=#000000]

        Using reader [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]New[/COLOR][COLOR=#000000] StreamReader[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]filename[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] line [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] reader[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]ReadToEnd[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]
            rtb_Subsidary[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]AppendText[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]file [/COLOR][COLOR=#000000]&[/COLOR][COLOR=#000000] vbCrLf[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
            DS[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Tables[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"MyTable"[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#000000]Rows[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]file[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"test 1"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"test 2"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"test 3"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"test 4"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"test 5"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"test 6"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] Using

        [/COLOR][COLOR=#00008B]With[/COLOR][COLOR=#000000] dgv_Lic
            [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]DataSource [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] DS[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Tables[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"MyTable"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#000000].[/COLOR][COLOR=#00008B]ReadOnly[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]True[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]ScrollBars [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] ScrollBars[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Vertical
            [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]AutoSizeColumnsMode [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] DataGridViewAutoSizeColumnsMode[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Fill
            [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]AutoResizeColumns[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]RowHeadersVisible [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]False[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#00008B]For[/COLOR][COLOR=#00008B]Each[/COLOR][COLOR=#000000] col [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] DataGridViewColumn [/COLOR][COLOR=#00008B]In[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Columns
                col[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]SortMode [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] DataGridViewColumnSortMode[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]NotSortable
            [/COLOR][COLOR=#00008B]Next[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]With[/COLOR][COLOR=#000000]



[/COLOR][COLOR=#000000]...[/COLOR][COLOR=#00008B]end[/COLOR][COLOR=#000000] code[/COLOR]
 
According to the StreamReader.ReadToEnd documentation, it "Reads the stream from the current position to the end of the stream.", so basically you're reading your entire input file into your line string object, then adding a row to your datatable.

Assuming you're trying to create a separate row for each 'line' in the input file, you'd need some form of loop to do that - a StreamReader.ReadLine coupled with a While loop should do the trick.
 
Back
Top