In ASP.Net, we use the data tables very frequently in our application. Sometimes
we need to delete the row from the data table using some filter condition.
Today I will show you a simple example to delete the row from your data table
based on your condition.
Deleting row in data table:
Now,
I want to delete the record in data table where ID is 5.
//select the record from data table where ID is 5
DataRow[]
dr=dtTestRecord.Select("ID=5");
//delete the data rows return by Select method of data table
foreach
(var row in dr)
row.Delete();
Creating extension method in data table for row delete:
You
can create an extension method in data table for deleting the row. Code for creating extension method is-
public static class DataTableExtensionMethods
{
public static DataTable
DeleteRow(this DataTable
table, string filter)
{
table.Select(filter).Delete();
table.AcceptChanges();
return
table;
}
public static void Delete(this IEnumerable<DataRow> rows)
{
foreach
(var row in
rows)
row.Delete();
}
}
Happy coding!!
No comments:
Post a Comment