Remove datarow from datatable

You should save a reference to each entry you want to delete in a new collection and then remove all the new entries from the old collection:

DataRow[] dr = payments.dtPayments.Select(myselect);
List<DataRow> rowsToRemove = new List<DataRow>();

for (int a = 0; a < dr.Length; a++) {
    if(/* You want to delete this row */) {
        rowsToRemove.Add(dr[a]);
    }
}

foreach(var dr in rowsToRemove) {
    payments.dtPayments.Rows.Remove(dr);
}

1 comment:

  1. You need to create a datarow outside the loop, then when you get to the row you want to remove assign it to the row you created outside. and break the loop. Then below the loop you can remove it.

    DataRow r = null;
    foreach(DataRow row in table.Rows)
    {
    if(condition==true)
    {
    r = row;
    break;
    }
    }

    table.Rows.Remove(r);

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...