Click here to Skip to main content
15,946,525 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to programmatically add a row to DataGridView (a datasource was
binded with it), but when I call the method DataGridView.Rows.Add, a
System.InvalidOperationException will be thrown and the error message is:
Rows cannot be programmatically added to the DataGridView's rows
collection when the control is data-bound.


C#
private void add_click(object sender, EventArgs e)
{
 try
 {
    DataRow row = db1.NewRow();
    dataGridView2.Rows.Add(dataGridView2.CurrentRow.Index);
    sda.Update(db1);
 }
 catch (Exception exceptionObj)
 {
   System.Windows.Forms.MessageBox.Show(exceptionObj.Message.ToString());
 }

}


What code change should be done to dynamically add a row to datagrid when it is bound to data source.?
Posted
Updated 4-Oct-11 20:12pm
v3

1 solution

You need to add the new row to the DataSource instead of the DataGridView.
 
Share this answer
 

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