DataGridView - how to delete Row
-
Hi all, I am working on C# project using C# 2.0. I have a DataGrid that contains 10 rows and at runtime I insert one more row at DataGridView.Insert(0,1); in my next step, I want to delete the row from DataGridView at DataGridView.RemoveAt(9) // Index 9 is the last row in the grid. But, I am not able to delete the row. I am getting following exception: System.InvalidOperationException:Uncommitted new row cannot be deleted. at System.Windows.Forms.DataGridViewRowCollection.RemoveAt(Int32 index) Please help if you know the solution. Thanks in advance for your help.
A.Asif
-
Hi all, I am working on C# project using C# 2.0. I have a DataGrid that contains 10 rows and at runtime I insert one more row at DataGridView.Insert(0,1); in my next step, I want to delete the row from DataGridView at DataGridView.RemoveAt(9) // Index 9 is the last row in the grid. But, I am not able to delete the row. I am getting following exception: System.InvalidOperationException:Uncommitted new row cannot be deleted. at System.Windows.Forms.DataGridViewRowCollection.RemoveAt(Int32 index) Please help if you know the solution. Thanks in advance for your help.
A.Asif
The easy way to delete the row of the data gird is to get reference of the current row. Note: I assume that Grid is bound to a BindingSource names "bindingSource". Put the following code inside the event of Delete button.
DataRowView DRV = bindingSource.Current; if(DRV!=null) // If any row is available in grid DRV.Delete();
Regards