DataTable, How to conditionally delete rows

You could query the dataset and then loop the selected rows to set them as delete. var rows = dt.Select(“col1 > 5”); foreach (var row in rows) { row.Delete(); } dt.AcceptChanges(); … and you could also create some extension methods to make it easier … myTable.Delete(“col1 > 5”); public static DataTable Delete(this DataTable table, string … Read more

Deleting specific rows from DataTable

If you delete an item from a collection, that collection has been changed and you can’t continue to enumerate through it. Instead, use a For loop, such as: for(int i = dtPerson.Rows.Count-1; i >= 0; i–) { DataRow dr = dtPerson.Rows[i]; if (dr[“name”] == “Joe”) dr.Delete(); } dtPerson.AcceptChanges(); Note that you are iterating in reverse … Read more