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


Active member
Mar 30, 2020
Programming Experience
I have a windows form named as FrmInvoiceEdit. In this form saved Invoice is displayed in datagridview1 by searching Invoice no. in text box which is named as TxtInvoiceNo.
The code is
 Dim cmd As New OleDbCommand("SELECT * from TblTest WHERE InvoiceID = @InvoiceID", con)

        With cmd.Parameters

            .Add("@InvoiceID", OleDbType.VarChar, 50).Value = TxtInvoiceNo.Text

            Dim da As New OleDbDataAdapter
            Dim dt As New DataTable
            da.SelectCommand = cmd
            DataGridView1.DataSource = dt
I have another form named as FrmAddItemsNew for adding, editing items. When AddItem button is clicked this form is open.
Add button code is
FrmInvoiceEdit.DataGridView1.Rows.Add(TxtInvoiceID.Text, TxtID.Text, CmbProductName.Text, TxtHSN.Text, TxtSerial.Text, TxtUnit.Text, TxtQty.Text, TxtInclRate.Text, TxtBaseRate.Text, TxtAmount.Text, TxtGST.Text, TxtGSTAmt.Text, TxtTotal.Text)
but when I click this button the error is shown as -
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
The code for editing row is
Private Sub Editrecord()
        If MsgBox("Edit Product Entry?", vbYesNo + vbQuestion) = vbYes Then
            Dim i As Integer
            i = DataGridView1.CurrentCell.RowIndex

            FrmAddItemsNew.TxtID.Text = DataGridView1.Rows(i).Cells(1).Value.ToString()
            FrmAddItemsNew.CmbProductName.Text = DataGridView1.Rows(i).Cells(2).Value.ToString()
            FrmAddItemsNew.TxtHSN.Text = DataGridView1.Rows(i).Cells(3).Value.ToString()
            FrmAddItemsNew.TxtSerial.Text = DataGridView1.Rows(i).Cells(4).Value.ToString()
            FrmAddItemsNew.TxtUnit.Text = DataGridView1.Rows(i).Cells(5).Value.ToString()
            FrmAddItemsNew.TxtQty.Text = DataGridView1.Rows(i).Cells(6).Value.ToString()
            FrmAddItemsNew.TxtInclRate.Text = DataGridView1.Rows(i).Cells(7).Value.ToString()
            FrmAddItemsNew.TxtBaseRate.Text = DataGridView1.Rows(i).Cells(8).Value.ToString()
            FrmAddItemsNew.TxtAmount.Text = DataGridView1.Rows(i).Cells(9).Value.ToString()
            FrmAddItemsNew.TxtGST.Text = DataGridView1.Rows(i).Cells(10).Value.ToString()
            FrmAddItemsNew.TxtGSTAmt.Text = DataGridView1.Rows(i).Cells(11).Value.ToString()
            FrmAddItemsNew.TxtTotal.Text = DataGridView1.Rows(i).Cells(12).Value.ToString()
        End If
And when I select row and press edit button to edit row the error is shown as -
Additional information: Object reference not set to an instance of an object.

I want user to add, edit and delete the item from Invoice.
When he add, edit and delete the item firstly it should be populated in datagridview and when he press button Save it should be saved to the database, but if he add, edit and delete the item but he want to cancel these changes by Cancel Button is pressed it should not be saved to the database, it should be only affect the datagridview.
I have seen this in professional softwares which are in market.
I searched many video but cannot find any answer for this.
any help will be appreciated.


If you bind data by setting the DataSource then you should be dealing with the data source in code, not the grid. You should also use a BindingSource. Bind your DataTable to the BindingSource and the BindingSource to the grid, then access the data via the BindingSource.
I am not that much expert in writing code. How to write this code and where ?
You don't have to be an expert in writing code to search the web. The idea is that, when someone mentions a topic, type or member that you are unfamiliar with, you go and do whatever research you can on that, use what you find to the best of your ability and then post back if and when you encounter an actual issue. What research have you done on the BindingSource? None, I would guess. If you haven't tried then you haven't failed so you don't know that you need any help at all. Do what you can for yourself, show or explain to us what you have done and then we can help you with the next problem you encounter. If you don't try then you haven't encountered a problem yet. We want to help but helping doesn't mean doing it for you. Show us that have tried and we will happily provide further assistance.