[Solved] Rows cannot be programmatically removed unless the DataGridView is data-bound to an IBindingList that supports change notification and allows deletion. When you bind the Datagridview with List in windows form. You get error when you delete row from it. According to article title, first to implement your List from IBindingList interface, if you want to solve this issue. Suppose, you want to bind your DataGridView with this class, which is mentioned below:
public partial class userAccount
{
public decimal Account_No { get; set; }
public string Name { get; set; }
}
}
Then you write simple code for this :
List<userAccount> ua= new List<userAccount>();
ua.Add(new userAccount(){Account_No=1000000008, Name ="Jacob"});
dataGridView1.DataSource = ua;
After binding the DataGridview, you will write the code for remove row from dataGridview.
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
After that you got error, which is mentioned below:
public partial class userAccount
{
public decimal Account_No { get; set; }
public string Name { get; set; }
}
}
Then you write simple code for this :
List<userAccount> ua= new List<userAccount>();
ua.Add(new userAccount(){Account_No=1000000008, Name ="Jacob"});
dataGridView1.DataSource = ua;
After binding the DataGridview, you will write the code for remove row from dataGridview.
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
After that you got error, which is mentioned below:
[Solution]
Bind DataGridView with IBindingList<userAccount> . Now the code is:
BindingList<userAccount> bi = new BindingList<userAccount> ();
bi.Add(new userAccount(){Account_No=1000000008, Name ="Jacob"});
dataGridView1.DataSource = bi;
dataGridView1.DataSource = bi;
After binding the DataGridview, you will write the code for remove row from dataGridview.
bi.RemoveAt(dataGridView1.SelectedRows[0].Index);