2

I am trying to delete a specific row from datatable. When adding the last row, I need to delete the yellow colored rows. It is very easy to Select from a datatable like below

DataRow[] dr = dt.Select("STOK_KODU='HAMMADDE_2'");

I was wondering if there is a way like below to delete ??? Or would you advice an easy way to delete a rows from datatable?

dt.Delete("STOK_KODU='HAMMADDE_2'");

enter image description here

1
  • 1
    Do you want to remove the rows from the in memory datatable or do you want to remove them from the backend database table? Commented Feb 4, 2014 at 12:06

4 Answers 4

4

One way is to recreate the table with the rows you want to keep:

dt = dt.AsEnumerable()
    .Where(row => row.Field<string>("STOK_KODU") != "HAMMADDE_2")
    .CopyToDataTable()

The other is to use DataRowCollection.Remove:

DataRow[] rowsToRemove = dt.Select("STOK_KODU='HAMMADDE_2'");
foreach (var rowToDelete in rowsToRemove)
    dt.Rows.Remove(rowToDelete);

The second approach is more efficient if you want to delete few rows and the table is large. The first approach using LINQ is more powerful since you can use any code but it can be less efficient.

Sign up to request clarification or add additional context in comments.

Comments

1

You can access the Rows collection of the DataTable:

foreach (var row in dr)
    dt.Rows.Remove(row);

Comments

1

try this ::

DataRow[] rows;
rows=dt.Select("STOK_KODU='HAMMADDE_2'"); 
foreach(DataRow r in rows)
r.Delete();

3 Comments

This will not remove the rows from the table and it will also not delete the rows in the database (without using a DataAdapter). It will just set the rowstate to Deleted which is a flag for a DataAdapter.
@TimSchmelter I was just writing about it. But the fastest gun in the west has hit again :-))
@ayilmaz Tim is right, if you want to remove rows, then you should use Rows.Remove
1

Deleting rows from an in memory DataTable object is really easy

dt.Select("STOK_KODU='HAMMADDE_2'").AsEnumerable().ToList().ForEach(x => x.Delete());

However you should consider that the Delete method simply marks the RowState to Deleted, but the rows are still in the DataTable.Rows collection. To really remove them you need to call

dt.AcceptChanges();

without this call, if you loop over the datatable rows collection, you need to check the RowState to avoid an error message stating that you cannot access the information of a deleted row

foreach(DataRow r in dt.Rows)
{

    if(r.RowState != DataRowState.Deleted)    
         Console.WriteLine(r[0].ToString()); 
}

1 Comment

DataTable.AcceptChanges does not remove the rows which have RowState=Deleted. It will make the RowState=Unchanged. It's just a flag for a DataAdapter which wil call AcceptChanges itself after it has updated the datasource. You can check that easily by inspecting dt.GetChanges() in the debugger before and after you've called AcceptChanges.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.