In my project I need to delete all records from a DataTable, so I tried this code :
while (DataTable1.Rows.Count > 0)
DataTable1.Rows[0].Delete();
but this is an infinite loop, it seems that after the Delete() statement the Rows.Count does not changes.
It appears the records are still there but marked for deletion.
So I change my count to this :
int count = DataTable1.Select("", "", DataViewRowState.CurrentRows).Count();
So now I can change my loop like this
int count = DataTable1.Select("","",DataViewRowState.CurrentRows).Count();
while (count > 0)
{
DataTable1.Rows[0].Delete(); // THIS IS WRONG NOW
count = DataTable1.Select("","",DataViewRowState.CurrentRows).Count();
}
Now the count does counts down as expected, but now I have a problem with the Rows[0].Delete() statement.
It will delete the same row over and over again.
So my question is, how can I find the first row that is not deleted, and delete it ?
In other words, the line that is commented with // THIS IS WRONG NOW what can I use at that line ?
Or is there a better way to do this ? I tried DataTable1.Clear() but that does not generates delete statements when doing adapter.Update(DataTable1);