4

I have a grid and when data is loaded in grid; I simply select a row and press edit button. On edit, new sub form is opened and values of row's cells are passed to controls on sub form. But, when I change some values on sub form and save them to be replaces in grid the error comes to me: Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound. What is cause of this error and how to overcome this.

Itz.Irshad
  • 1,014
  • 5
  • 23
  • 44

3 Answers3

4

The cause is that "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound". It is as clear as crystalline water

The solution? Do not add rows to the DataGridView's rows collection, add them to the underlying datasource collection (the collection you are setting in to the DataSource property of the DataGridView)

Luxspes
  • 6,268
  • 2
  • 28
  • 31
1

Add or Edit your row in DataSource of your DataGridView. Do not directly add/edit to your grid.

If your DataSource is DataSet and you want to add new row

DataSet dsTempDataTable = (DataSet)MainApplication.dgvBooksDetails.DataSource;
DataTable dt = dsTempDataTable.Tables[0]; // use table index/name to get the exact table
DataRow dr = dt.NewRow();
//  code to fill record
dt.Rows.Add(dr);

To Edit

DataSet dsTempDataTable = (DataSet)MainApplication.dgvBooksDetails.DataSource;
DataTable dt = dsTempDataTable.Tables[0]; // use table index/name to get the exact table
dt.Rows[0]["columnName"] = "some value";
// your row edit  code
dt.AcceptChanges();
Talha
  • 18,898
  • 8
  • 49
  • 66
  • 1
    dt.Row[0] : here 0 indicates the index of row ? And how I can handle more then one columns in row ? I mean, hwo to set values to cells if I have 7 columns in a Row ? – Itz.Irshad Jul 12 '12 at 05:37
  • @Itz.Irshad very simple, you can use the column name like that.. dt.Rows[0]["column1"] = "value1"; dt.Rows[0]["column2"] = "value2"; dt.Rows[0]["column3"] = "value3"; and so on – Talha Jul 12 '12 at 05:57
  • Yeah, I did. But, since I have a `DataSet` as a `DataSource` to `Grid`....so getting this error: *Unable to cast object of type 'System.Data.DataSet' to type 'System.Data.DataTable'.* on first line having code: `DataTable dtTempDataTable = (DataTable)MainApplication.dgvBooksDetails.DataSource;` – Itz.Irshad Jul 12 '12 at 06:00
  • @Itz.Irshad see my updated answer, it converts the dataSource into DataSet object and than use the index to get the exact table – Talha Jul 12 '12 at 06:05
  • Well, I'm reading **xml file** via 'DataSet' and Providing it as 'DataSource' to 'Grid'. So, no tables is mentioned there. But, when as you suggest in latest code. use table in 'DataSet' and add row in it. I'm getting this error: **There is no row at position 1.** *I think it is because, in data is loaded from `DataSet` and newly created Table have no rows.* How it can be solved ? – Itz.Irshad Jul 12 '12 at 06:19
  • @Itz.Irshad debug your code and check `DataSet` hs any tables? and tables has any rows? – Talha Jul 12 '12 at 06:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13767/discussion-between-itz-irshad-and-talha) – Itz.Irshad Jul 12 '12 at 06:52
0

I had the same problem and I found solution.

//create datatable and columns
DataTable dtable = new DataTable();
dtable.Columns.Add(new DataColumn("Column 1"));
dtable.Columns.Add(new DataColumn("Column 2"));

//simple way create object for rowvalues here i have given only 2 add as per your requirement
object[] RowValues = { "", "" };

//assign values into row object
RowValues[0] = "your value 1";
RowValues[1] = "your value 2";

//create new data row
DataRow dRow;
dRow = dtable.Rows.Add(RowValues);
dtable.AcceptChanges();

//now bind datatable to gridview... 
gridview.datasource=dtable;
gridview.databind();

Source: http://www.codeproject.com/Questions/615379/Adding-rows-to-datagridview-with-existing-columns

Mega
  • 557
  • 2
  • 10
  • 22