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

Now I am using Select and using criteria that only selects new rows. But is there any GetInsertedRows method. If I remember correctly, there is a status for every line, so naturally it is possible to loop through them all, but that is not graceful.

-Chers-Matti

+2


a source to share


2 answers


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

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

      



You can also create an additional DataView to view new or deleted rows, and then these views will not byte each other:

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

      

+2


a source


I ran into this problem a while ago, however there is no good way to pull the added lines. I was just wasting my repos for you and found the implementation DataTable

I used:

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 still iterates, but it's nicely wrapped like IEnumerable

, and you don't have to write the code more than once.

+1


a source







All Articles