I tried to loop through my dataGridView1
and remove rows which don't satisfy the condition as following:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0)
{
dataGridView1.Rows.Remove(row); //error: Uncommitted new row cannot be deleted.
}
}
But I got this error:
Uncommitted new row cannot be deleted.
I can manage if the code also VB.NET.
try with putting following condition:
Don't use
foreach
in this case, the looped collection may be modified and leads to unpredicted result, sometimes throws exception like collection was modified (encountered mainly inLINQ
), usefor
instead:Note that we have to loop from the largest index to 0.
Try