How to delete all rows from datatable

meetjaydeep picture meetjaydeep · Apr 19, 2011 · Viewed 39.4k times · Source

I want to delete all rows from datatable with rowstate property value Deleted.

DataTable dt;
dt.Clear(); // this will not set rowstate property to delete.

Currently I am iterating through all rows and deleting each row.

Is there any efficient way? I don't want to delete in SQL Server I want to use DataTable method.


Answer

TcKs picture TcKs · Apr 19, 2011

We are using this way:

for(int i = table.Rows.Count - 1; i >= 0; i--) {
    DataRow row = table.Rows[i];
    if ( row.RowState == DataRowState.Deleted ) { table.Rows.RemoveAt(i); }
}