Click here to Skip to main content
15,569,774 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created a data table dbVisitor with empty rows and with two columns
then I create datagridview and put datagridview.datasource=dtVisitor;

then I write this code

dataGridView1.Rows.Add();
dataGridView1.Rows[0].Cells[0].Value = "1";
dataGridView1.Rows[0].Cells[1].Value = "2";
I got this error

"System.InvalidOperationException: 'Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."

so how can I addrows to a datagridview while this grid is bounded to an empty datatable

What I have tried:

.................................................................................................................................................................................................
Posted
Updated 27-Jan-18 4:10am

1 solution

The datatable is the data source for your grid view. So, if you want to add items to the view, you have to add them to its data source.
You should use DataTable.NewRow Method[^] instead.
Not knowing the schema of the datatable, it is hard to provide a more concrete answer, but the link holds an example of method's usage, and you can start with it and see where it gets you.
Hope this helps.
 
Share this answer
 
Comments
hussein hammoud1990 27-Jan-18 12:52pm    
thank you

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