Partager via


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.

Note

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.

    Dim changedRecords As DataSet = DataSet1.GetChanges()
    
    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.

    Dim changedRecordsTable As DataTable = dataTable1.GetChanges()
    
    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.

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

    Private Function GetNewRecords() As NorthwindDataSet.CustomersDataTable
    
        Return CType(NorthwindDataSet1.Customers.GetChanges(Data.DataRowState.Added),
            NorthwindDataSet.CustomersDataTable)
    End Function
    
    private NorthwindDataSet.CustomersDataTable GetNewRecords()
    {
        return (NorthwindDataSet.CustomersDataTable)
            northwindDataSet1.Customers.GetChanges(DataRowState.Added);
    }
    

See Also

Tasks

How to: Check for Changed Rows

How to: Get Specific Versions of a DataRow

Concepts

Binding Windows Forms Controls to Data in Visual Studio

Preparing Your Application to Receive Data

Fetching Data into Your Application

Binding Controls to Data in Visual Studio

Editing Data in Your Application

Validating Data

Saving Data

Other Resources

Connecting to Data in Visual Studio