views:

78

answers:

2

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

+1  A: 

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.

GenericTypeTea
yes, this is nice. I however wonder if my Select implementation is faster? my is DataTable.Select("Id < 0"); since new rows have id-field less than zero just before updating to real db-scope-uniq ids. i also wonder why ADO.NET lacks this method?
matti
+2  A: 

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;
Henk Holterman
cheers! this i didn't know. i haven't been using DataViews yet.
matti
+1 DataViews! It's been so long since I've used DataSets and DataTables I totally forgot about this one.
GenericTypeTea