char m
char m

Reputation: 8336

how to get rows created with DataTable.NewRow() in C# / ADO.NET 2.0?

Now I use Select and use a criteria which select only new rows. But is there any kind of GetInsertedRows-method. If I remember correctly there is status for each row so naturally one can loop through them all but that's not elegant.

-Cheers -Matti

Upvotes: 2

Views: 567

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273581

I like TypeT's answer but it may help to know that you always bind through a DataView to a DataTable and you can set it to filter on the rows state:

myDataSet.myTable.DefaultView.RowStateFilter = DataViewRowState.Added;

You can also create an additional DataView to look at the new or deleted rows, and then those Views won't byte each other:

var addedView = new DataView(myDataSet.myTable);
addedView.RowStateFilter = DataViewRowState.Added;

Upvotes: 2

djdd87
djdd87

Reputation: 68506

I came across this issue myself a while ago, however there's no nice way of pulling out the added rows. I've just trawled my repositories for you and found the DataTable implementation I used to use:

public class AdvancedDataTable : DataTable
{
    public IEnumerable<DataRow> InsertedRowList
    {
        get
        {
            foreach (DataRow row in this.Rows)
            {
                if (row.RowState == System.Data.DataRowState.Added)
                {
                    yield return row;
                }
            }
        }
    }
}

It's still doing an iteration, but it's nicely wrapped as an IEnumerable and you won't have to write the code more than once.

Upvotes: 1

Related Questions