Click here to Skip to main content
15,076,130 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Sir,

I have two Datagridviews which get results from database (dynamically). both Datagridview have equal no of columns with first column as checkbox:

DataGridViewCheckBoxColumn CBColumn2 = new DataGridViewCheckBoxColumn();
CBColumn2.HeaderText = "Confirm";
CBColumn2.FalseValue = false;
CBColumn2.TrueValue = true;
dataGridView2.Columns.Insert(0, CBColumn2);

I want to move selected single row from Datagridview 2 to Datagridview 1. I have used following code segement. It's working to remove the selected data, but it's not moving and getting following error message from VS2008 "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."

Here is the code segment, please help me. Thank You!

foreach (DataGridViewRow selRow in dataGridView2.SelectedRows.OfType<DataGridViewRow>().ToArray())



1 solution

The error message is pretty explicit:
"Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."

All it's saying is that you can't add rows to a databound control - for the obvious reason that it doesn't know what to do with them!
See here:[^] - it explains it and the solution.
abdulsafran 16-Nov-14 6:54am
Dear OriginalGriff, Thank you for your reply, I have modified the code like below:
<pre>foreach (DataGridViewRow selRow in dataGridView2.SelectedRows.OfType<datagridviewrow>().ToArray())



But still getting same error, if you don't mind can you able to edit my code and let me know. Thanks!
OriginalGriff 16-Nov-14 6:59am couldn't be bothered to follow the link and read what it says?
abdulsafran 16-Nov-14 7:52am
Dear Sir,

I have spend 2 days for this issue, I couldn't able to get proper answer, please help me.......
abdulsafran 16-Nov-14 7:08am
I followed the link, but I'm little bit confusion on how to use following code segment to my one:

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.

If you can, please support me. Because new row method is required to add binding datagridview.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900