priyanka

priyanka

  • 1.6k
  • 215
  • 24.6k

delete rows in datagridview in c#

Aug 11 2018 1:14 AM
I want to delete some rows from datagridview based on condition in c#,but at run time i find that row,though by debugging  my code is working properly.
 
My code is as follows:-
foreach (DataGridViewRow item in this.dataGridView1.Rows)
{
if ((item.Cells["Start Time"].Value.ToString() != "22:00:00") || (item.Cells["Start Time"].Value.ToString() != "18:00:00"))
{
dataGridView1.Rows.Remove(item);
}
}
 
dataGridView1.Rows.Remove(item);
Instead of this line,I have even tried
 dataGridView1.Rows.RemoveAt(item.Index)
 
i have even tried to delete rows from datatable  and then bind in datagridview ,but nothings helped me.
why my above code fails to delete record in datagridview,PLZ help me out. 

Answers (14)