How to: Check for Changed Rows

When changes are made to records in a dataset, information about those changes is stored until you commit them. Changes are committed when calling the AcceptChanges method of a dataset, data table, or calling the Update method of a TableAdapter or data adapter.

Changes are tracked in each data row two ways:

Determining If There Are Changed Rows

The HasChanges method of a dataset returns true if changes have been made in the dataset. After determining that changed rows exist, you can call the GetChanges method of a DataSet or DataTable to return a set of changed rows. For more information, see How to: Retrieve Changed Rows.

To determine if any changes have been made to any rows

  • Call the HasChanges method of a dataset to check for changed rows.

    The following example shows how to check the return value from the HasChanges method to detect whether there are any changed rows in a dataset named NorthwindDataset1.

    If NorthwindDataSet1.HasChanges() Then 
    
        ' Changed rows were detected, add appropriate code. 
    Else 
        ' No changed rows were detected, add appropriate code.  
    End If
    
    if (northwindDataSet1.HasChanges()) 
    {
        // Changed rows were detected, add appropriate code.
    }
    else
    {
        // No changed rows were detected, add appropriate code.
    }
    

Determining the Type of Changes

You can also check to see what type of changes have been made in a dataset by passing a value from the DataRowState enumeration to the HasChanges method.

To determine what type of changes have been made to a row

  • Pass a DataRowState value to the HasChanges method.

    The following example shows how to check a dataset named NorthwindDataset1 to determine if there have been any new rows added to it:

    If NorthwindDataSet1.HasChanges(DataRowState.Added) Then 
    
        ' New rows have been added to the dataset, add appropriate code. 
    Else 
        ' No new rows have been added to the dataset, add appropriate code. 
    End If
    
    if (northwindDataSet1.HasChanges(DataRowState.Added)) 
    {
        // New rows have been added to the dataset, add appropriate code.
    }
    else
    {
        // No new rows have been added to the dataset, add appropriate code.
    }
    

See Also

Concepts

Editing Data in Your Application

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

Validating Data

Saving Data

Other Resources

Data Walkthroughs

Connecting to Data in Visual Studio