简体   繁体   中英

Remove rows from a DataTable

I need to remove rows from a DataTable dt where a column have a specific value. Before I remove them I copy them to a other DataTable dt2nd.

It looks like this

        for (int i = dt.Rows.Count - 1; i >= 0; i--)
        {
            DataRow row = dt.Rows[i];

            if (row["Column2"].ToString() == "This Value")
            {
                dt2nd.ImportRow(row); //Copy
                dt.Rows.Remove(row); //Remove
            }
            if (row["Column2"].ToString() == "Other Value")
            {
                dt2nd.ImportRow(row); //Copy
                dt.Rows.Remove(row); //Remove
            }
            //This continues with more if
        }

But I get a error message

"Additional information: This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.".

Lots of rows can have the "This Value" or "Other Value".

The ==" " values can be a lot of different values so if I do it like this it will be a lot of IF statements.

Is there any good way to do this, with LINQ or some other way?

This code will work :

for (int i = dt.Rows.Count - 1; i >= 0; i--)
    {
        DataRow row = dt.Rows[i];

        if (row["Column2"].ToString() == "This Value")
        {
            dt2nd.ImportRow(row); //Copy
            dt.Rows.Remove(row); //Remove
            continue;
        }
        if (row["Column2"].ToString() == "Other Value")
        {
            dt2nd.ImportRow(row); //Copy
            dt.Rows.Remove(row); //Remove
            continue; 
        }
        //This continues with more if
    }

or you can use a switch statement instead of all the ifs or use else if

for (int i = dt.Rows.Count - 1; i >= 0; i--)
    {
        DataRow row = dt.Rows[i];

        if (row["Column2"].ToString() == "This Value")
        {
            dt2nd.ImportRow(row); //Copy
            dt.Rows.Remove(row); //Remove
        }
        else if (row["Column2"].ToString() == "Other Value")
        {
            dt2nd.ImportRow(row); //Copy
            dt.Rows.Remove(row); //Remove
        }
        //This continues with more else if
    }

or

for (int i = dt.Rows.Count - 1; i >= 0; i--)
    {
        DataRow row = dt.Rows[i];

        switch(row["Column2"].ToString()){
        case "This Value":
        {
            dt2nd.ImportRow(row); //Copy
            dt.Rows.Remove(row); //Remove
            break;
        }
        case "Other Value":
        {
            dt2nd.ImportRow(row); //Copy
            dt.Rows.Remove(row); //Remove
            break; 
        }
        //This continues with more case
    }

But you can also use a select on your datatable and iflter out all the rows that need to be moved out. something like this :

dt.select("Column2 == 'This value' or ...");

it will return the rows that satisfy the condition. See example here : http://www.dotnetperls.com/datatable-select

But the code can be made a lot shorter if the action is always the same :

for (int i = dt.Rows.Count - 1; i >= 0; i--)
    {
        DataRow row = dt.Rows[i];

        if (
               (row["Column2"].ToString() == "This Value") ||
               (row["Column2"].ToString() == "Other Value") //more checks
           )
        {
            dt2nd.ImportRow(row); //Copy
            dt.Rows.Remove(row); //Remove
        }
    }

or

for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
    DataRow row = dt.Rows[i];

    switch(row["Column2"].ToString()){
    case "This Value":
    case "Other Value": //more case values
    {
        dt2nd.ImportRow(row); //Copy
        dt.Rows.Remove(row); //Remove
        break;
    }
}

You can try this...

        var checkValues = new string[]  { "Some value1", "Some value3" };
        DataTable dt = new DataTable();
        DataTable dt1 = new DataTable();
        dt.Columns.Add("Column1");
        dt.Columns.Add("Column2");
        dt.Columns.Add("Column3");
        dt.Rows.Add(new string[] {"Some value1", "Some value2", "Some value3" });
        dt.AsEnumerable().ToList().ForEach(x =>
            {
                if (checkValues.Contains(x["Column1"]))
                {
                    dt1.ImportRow(x);
                    dt.Rows.Remove(x);
                }
            });
var valuesToBeChecked = new String[] { "value1", "value2" };
        var x = from y in dt.AsEnumerable()
                where valuesToBeChecked.Any(t => valuesToBeChecked.Contains(y["Column2"].ToString()))
                select y;
        var z = dt.AsEnumerable().Except(x);

Then, you can copy z to datatable you want:

DataTable dt2 = z.CopyToDataTable();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM