Export (0) Print
Expand All

How to: Retrieve Changed Rows

Each row in a data table has a RowState property that keeps track of the current state of that row using the values in the DataRowState enumeration. You can return changed rows from a dataset or data table by calling the GetChanges method of a DataSet or DataTable. You can verify changes exist prior to calling GetChanges by calling the HasChanges method of a dataset. For more information on HasChanges, see How to: Check for Changed Rows.

NoteNote:

After you commit changes to a dataset or data table (by calling the AcceptChanges method), the GetChanges method will return no data. If your application needs to process changed rows, you must do so before calling the AcceptChanges method.

Calling the GetChanges method of a dataset or data table returns a new dataset or data table that contains only records that have been changed. If you want to get only specific records — for example, only new records or only modified records — you can pass a value from the DataRowState enumeration as a parameter to the GetChanges method.

Use the DataRowVersion enumeration to access the different versions of a row (for example, you might want to examine the original values in a row prior to processing it.

To get all changed records from a dataset

  • Call the GetChanges method of a dataset.

    The following example creates a new dataset called changedRecords and populates it with all the changed records from another dataset called dataSet1.

    DataSet changedRecords = dataSet1.GetChanges();
    

To get all changed records from a data table

  • Call the GetChanges method of a DataTable.

    The following example creates a new data table called changedRecordsTable and populates it with all the changed records from another data table called dataTable1.

    DataTable changedRecordsTable = dataTable1.GetChanges();
    

To get all records that have a specific row state

  • Call the GetChanges method of a dataset or data table and pass a DataRowState enumeration value as an argument.

    The following example shows how to create a new dataset called addedRecords and populate it only with records that have been added to the dataSet1 dataset.

    DataSet addedRecords = dataSet1.GetChanges(DataRowState.Added);
    
  • The following example shows how to return all records recently added to the Customers table:

    private NorthwindDataSet.CustomersDataTable GetNewRecords()
    {
        return (NorthwindDataSet.CustomersDataTable)
            northwindDataSet1.Customers.GetChanges(DataRowState.Added);
    }
    

Community Additions

ADD
Show:
© 2014 Microsoft