0

I'm having trouble adding a row to my datagridview for a redo button. I want to add the public string variable called DeletedFilmName to a new row in the first column (column is called "Films"), that's it. I've tried all sorts of things but can't seem to get this working, as simple as it seems.

Here's my error: "InvalidOperationException was unhandled. Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."

Private Sub Redo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Redo.Click
    FilmsDataGridView.Rows.Add(New Object() {DeletedFilmName})
End Sub

Any ideas? Thanks for your help in advance.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
user2100364
  • 87
  • 1
  • 1
  • 15
  • 1
    the error is pretty clear: when the DGV is bound to a data source, you cannot add rows to the DGV - add it to the datasource instead. All bound controls are like that. – Ňɏssa Pøngjǣrdenlarp Dec 02 '13 at 02:44

1 Answers1

0

The following code is an exmaple which explains how to add a new row to the DataGridView binds to a DataTable.

Code Snippet

        // DataGridView will generate a new row automatically
        SampleDataSet.CustomersRow newCustomersRow = sampleDataSet.Customers.NewCustomersRow();
        newCustomersRow.CustomerID = "1234-123";
        newCustomersRow.CompanyName = "Sample Works";
        sampleDataSet.Customers.Rows.Add(newCustomersRow);

Regards Ruban.J

Ruban J
  • 622
  • 1
  • 7
  • 31
  • I get a whole lot of "not a member of" and "undeclared" errors. I think the "CustomersRow" would be a good place for me to ask, "Huh?" I have code like: `Dim myRow As DataRow = MyTable.NewRow myRow("Proj_InstId") = DataGridView1.Rows(dgvrowCnt).Cells(0).Value + 1 MyTable.Rows.Add(myRow) InitializeDataGridView()` that also doesn't work but no errors are thrown. – ZL1Corvette Jan 07 '14 at 16:50