DataSet.GetChanges Method

Definition

Gets a copy of the DataSet containing all changes made to it since it was last loaded, or since AcceptChanges() was called.

Overloads

GetChanges()

Gets a copy of the DataSet that contains all changes made to it since it was loaded or since AcceptChanges() was last called.

GetChanges(DataRowState)

Gets a copy of the DataSet containing all changes made to it since it was last loaded, or since AcceptChanges() was called, filtered by DataRowState.

GetChanges()

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

Gets a copy of the DataSet that contains all changes made to it since it was loaded or since AcceptChanges() was last called.

C#
public System.Data.DataSet? GetChanges();
C#
public System.Data.DataSet GetChanges();

Returns

A copy of the changes from this DataSet that can have actions performed on it and later be merged back in using Merge(DataSet). If no changed rows are found, the method returns null.

Examples

The following example creates a simple DataSet with one table, two columns, and ten rows. Two values are changed, and one row is added. A subset of the changed data is created using the GetChanges method. After reconciling errors, a new column is added to the subset, changing the schema. When the Merge method is called with the missingSchemaAction set to MissingSchemaAction.Add, the new column is added to the original DataSet object's schema.

C#
private void DemonstrateMerge()
{
    // Create a DataSet with one table, two columns,
    // and three rows.
    DataSet dataSet = new DataSet("dataSet");
    DataTable table = new DataTable("Items");
    DataColumn idColumn = new DataColumn("id",
        Type.GetType("System.Int32"),"");
    idColumn.AutoIncrement=true;
    DataColumn itemColumn = new DataColumn("Item",
        Type.GetType("System.Int32"),"");

    // DataColumn array to set primary key.
    DataColumn[] keyColumn= new DataColumn[1];
    DataRow row;

    // Create variable for temporary DataSet.
    DataSet changesDataSet;

    // Add RowChanged event handler for the table.
    table.RowChanged+=new DataRowChangeEventHandler(
        Row_Changed);
    dataSet.Tables.Add(table);
    table.Columns.Add(idColumn);
    table.Columns.Add(itemColumn);

    // Set primary key column.
    keyColumn[0]= idColumn;
    table.PrimaryKey=keyColumn;
    // Add ten rows.
    for(int i = 0; i <10;i++)
    {
        row=table.NewRow();
        row["Item"]= i;
        table.Rows.Add(row);
    }
    // Accept changes.
    dataSet.AcceptChanges();
    PrintValues(dataSet, "Original values");

    // Change row values.
    table.Rows[0]["Item"]= 50;
    table.Rows[1]["Item"]= 111;

    // Add one row.
    row=table.NewRow();
    row["Item"]=74;
    table.Rows.Add(row);

    // Insert code for error checking. Set one row in error.
    table.Rows[1].RowError= "over 100";
    PrintValues(dataSet, "Modified and New Values");

    // If the table has changes or errors,
    // create a subset DataSet.
    if(dataSet.HasChanges(DataRowState.Modified |
        DataRowState.Added)&& dataSet.HasErrors)
    {
        // Use GetChanges to extract subset.
        changesDataSet = dataSet.GetChanges(
            DataRowState.Modified|DataRowState.Added);
        PrintValues(changesDataSet, "Subset values");

        // Insert code to reconcile errors. In this case, reject changes.
        foreach(DataTable changesTable in changesDataSet.Tables)
        {
            if (changesTable.HasErrors)
            {
                foreach(DataRow changesRow in changesTable.Rows)
                {
                    //Console.WriteLine(changesRow["Item"]);
                    if((int)changesRow["Item",DataRowVersion.Current ]> 100)
                    {
                        changesRow.RejectChanges();
                        changesRow.ClearErrors();
                    }
                }
            }
        }
        // Add a column to the changesDataSet.
        changesDataSet.Tables["Items"].Columns.Add(
            new DataColumn("newColumn"));
        PrintValues(changesDataSet, "Reconciled subset values");
        // Merge changes back to first DataSet.
        dataSet.Merge(changesDataSet, false,
            System.Data.MissingSchemaAction.Add);
    }
    PrintValues(dataSet, "Merged Values");
}

private void Row_Changed(object sender, DataRowChangeEventArgs e)
{
    Console.WriteLine("Row Changed " + e.Action.ToString()
        + "\table" + e.Row.ItemArray[0]);
}

private void PrintValues(DataSet dataSet, string label)
{
    Console.WriteLine(label + "\n");
    foreach(DataTable table in dataSet.Tables)
    {
        Console.WriteLine("TableName: " + table.TableName);
        foreach(DataRow row in table.Rows)
        {
            foreach(DataColumn column in table.Columns)
            {
                Console.Write("\table " + row[column] );
            }
            Console.WriteLine();
        }
    }
}

Remarks

Creates a new DataSet that contains a copy of all rows in the original DataSet that have pending changes. Relationship constraints can cause additional unchanged rows to be added to the new DataSet if the unchanged rows contain primary keys corresponding to foreign keys in the changed rows. The method returns null if there are no rows in the original DataSet that have pending changes.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetChanges(DataRowState)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

Gets a copy of the DataSet containing all changes made to it since it was last loaded, or since AcceptChanges() was called, filtered by DataRowState.

C#
public System.Data.DataSet? GetChanges(System.Data.DataRowState rowStates);
C#
public System.Data.DataSet GetChanges(System.Data.DataRowState rowStates);

Parameters

rowStates
DataRowState

One of the DataRowState values.

Returns

A filtered copy of the DataSet that can have actions performed on it, and subsequently be merged back in using Merge(DataSet). If no rows of the desired DataRowState are found, the method returns null.

Examples

The following example uses the GetChanges method to create a second DataSet object, which is then used to update a data source.

C#
private void UpdateDataSet(DataSet dataSet)
{
    // Check for changes with the HasChanges method first.
    if(!dataSet.HasChanges(DataRowState.Modified)) return;

    // Create temporary DataSet variable and
    // GetChanges for modified rows only.
    DataSet tempDataSet =
        dataSet.GetChanges(DataRowState.Modified);

    // Check the DataSet for errors.
    if(tempDataSet.HasErrors)
    {
        // Insert code to resolve errors.
    }
    // After fixing errors, update the data source with
    // the DataAdapter used to create the DataSet.
    adapter.Update(tempDataSet);
}

Remarks

The GetChanges method is used to produce a second DataSet object that contains only the changes introduced into the original. Use the rowStates argument to specify the type of changes the new object should include.

This returned copy is designed to be merged back in to this original DataSet. Relationship constraints may cause parent rows marked Unchanged to be included. If no rows of the desired DataRowState are found, the GetChanges method returns null.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1