Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound

First of all, I looked up this related question in here but the solution dataGridView1.Rows.Add() doesn't work in my case.

In my Datagridview, I have 3 TextBoxes for data input and 2 ComboBoxes for the user to choose the values (which are bound into the database). One of my TextBoxes is set to read only so that the users can only populate it outside the datagrid (with a normal TexBox and a Button).

When the users fill a DataGridView with data, there is always an empty row at the bottom; so I disable this and I used this code to prevent the users from adding a new row inside the datagrid...

dataGridView1.AllowUserToAddRows = false

I only want to add a new row when the users click the button I mentioned above (which throws an error).

The error message I got was:

"Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound"

sample image the one with a red arrow is a ComboBox, and the one with green arrow is a read only TextBox

like image 550
Aurelius Anugrah Sugianto Avatar asked Jan 03 '12 04:01

Aurelius Anugrah Sugianto


3 Answers

It appears as though you are using the DataSource property of the DataGridView. When this property is used to bind to data you cannot explicitly add rows directly to the DataGridView. You must instead add rows directy to your data source.

For example if your data source is a DataTable, using the DataTable that is assigned to the DataSource property (untested):

private void AddARow(DataTable table)
{
    // Use the NewRow method to create a DataRow with 
    // the table's schema.
    DataRow newRow = table.NewRow();

    // Add the row to the rows collection.
    table.Rows.Add(newRow);
}
like image 113
Kerry H Avatar answered Nov 07 '22 17:11

Kerry H


You can get the DataGridView's DataSource and cast it as a DataTable.

Then add a new DataRow and set the fields' values.

Add the new row to the DataTable and Accept the changes.

In C# it would be something like this:

DataTable dataTable = (DataTable)dataGridView.DataSource;
DataRow drToAdd = dataTable.NewRow();

drToAdd["Field1"] = "Value1";
drToAdd["Field2"] = "Value2";

dataTable.Rows.Add(drToAdd);
dataTable.AcceptChanges();
like image 45
luchezco Avatar answered Nov 07 '22 18:11

luchezco


After adding a new row, you have to set the row index in boundary of row count. You have to do these steps.

  1. First, add row in DataGridView:

    dataGridView1.Rows.Add();
    
  2. Second, set new row index to count - 1:

    int RowIndex = dataGridView1.RowCount - 1;
    
  3. Then at last, set the controls values in it:

    DataGridViewRow R = dataGridView1.Rows[RowIndex];
    R.Cells["YourName"].Value = tbName.Text;
    

And if your datagrid's source is datattable you have to add row in that table.Give new values to the newly added row in data table and at last rebind the datagrid with updated datatable.

    DataRow row = dt.NewRow();  
    row["columnname"] = tbName.Text.toString();  
    dt.Rows.Add(row);
    dt.AcceptChanges();  

   dataGridView1.DataSource = dt;  
   dataGridView1.DataBind();

Check if you have set the index of the new row properly. Perhaps that's why you are getting this error.

like image 1
Syeda Avatar answered Nov 07 '22 17:11

Syeda