Find and delete a specific row from datatable


If you want to delete specific, multiple or single row delete from data table using by specific condition. Try to below code. You can delete multiple or single row from data table. There is many ways to delete rows from data table. Try Below code.

Try to below code. You can delete multiple or single row from data table. There is many ways to delete row(s) from data table.
1.

Delete multiple rows.




string _filterName_needed="approved";
DataRow[] drr = dt.Select("status=' "+ _filterName_needed +" ' ");
for (int i = 0; i < drr.Length; i++)
{
dt.Rows.Remove(drr[i]);
}


.....................................................

Delete only row.



string _filterName_needed="approved";
object obj = dt.Select("status=' "+_filterName_needed+" ' ");
DataRow drr = dt.Rows.Find(obj);
if (drr!=null)
{
drr.Delete();
}


**but you must know that your table must have a primery key to use this method.

Delete row with for loop



for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
if (dt.Rows[i]["status"].ToString() == "approved")
{
dt.Rows[i].Delete();
}
}


I hope it will be useful to developer..


Comments

Author: ketan Italiya02 Aug 2013 Member Level: Gold   Points : 0

so useful for beginners.thanks for post



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: