user3388946
user3388946

Reputation: 49

Add row to datagridview in vb.net

When I try add an extra row to my datagridview I get following error:

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

Any idea to fix this, without databinding I added rows like this:

    ' Populate the rows.
    Dim row() As String = {omschrijving, aantalstr, eenheidsprijs, basisbedrag, kortingstr, kortingbedrag, netto, btw, btwbedrag, totaal, productid}

    DataGridView1.Rows.Add(row)

Upvotes: 1

Views: 9273

Answers (1)

Anthony
Anthony

Reputation: 387

It looks like your grid view is bound to a data object. In that case, you need to add the row to the object it is bound to, like a dataset.

For instance, a rough example would be:

Dim boundSet As New DataSet

Dim newRow As DataRow = boundSet.Tables(0).NewRow
With newRow
    .Item(0) = "omschrijving"
    .Item(1) = "aantalstr"
    ...
End With

boundSet.Tables(0).Rows.Add(newRow)
boundSet.AcceptChanges()

You would just need to use the dataset that was bound to your grid view instead of creating a new one.

Upvotes: 2

Related Questions