0
votes

I'm trying to populate a datagrid view with the contents af a text file i used the following code:

Private Sub Button15_Click(sender As Object, e As EventArgs) _
                                                           Handles Button15.Click
  'strPath is the location of text file
  Dim lines = (From line In IO.File.ReadAllLines(strPath)
               Select line.Split(CChar(vbTab))).ToArray
  For x As Integer = 0 To lines(0).GetUpperBound(0)
    dgQuotation.Columns.Add(lines(0)(x), lines(0)(x))
  Next
  For x As Integer = 1 To lines.GetUpperBound(0)
    dgQuotation.Rows.Add(lines(x))
  Next
End Sub

But every time i run the program i get the following run time error:

Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.

Please help, I'm new to VB. Thanks in advance.

2
You must add the new row/s in the datasource of the datagrid view.Jade

2 Answers

0
votes

It means you either need to use data binding or add rows manually - pick one, not both.

0
votes

instead of the two for loops you can just set dgQuotation.DataSource = lines ... adding values to the grid programmatically (as you do in your code) is not possible if the grid is data bound (=> DataSource is set)