DataTable.Merge Method


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Merge the specified DataTable with the current DataTable.

Namespace:   System.Data
Assembly:  System.Data (in System.Data.dll)


Merge the specified DataTable with the current DataTable.

System_CAPS_pubmethodMerge(DataTable, Boolean)

Merge the specified DataTable with the current DataTable, indicating whether to preserve changes in the current DataTable.

System_CAPS_pubmethodMerge(DataTable, Boolean, MissingSchemaAction)

Merge the specified DataTable with the current DataTable, indicating whether to preserve changes and how to handle missing schema in the current DataTable.

The Merge method is used to merge two DataTable objects that have largely similar schemas. A merge is typically used on a client application to incorporate the latest changes from a data source into an existing DataTable. This allows the client application to have a refreshed DataTable with the latest data from the data source.

The merge operation takes into account only the original table, and the table to be merged. Child tables are not affected or included. If a table has one or more child tables, defined as part of a relationship, each child table must be merged individually.

The following console application demonstrates the behavior of the missingSchemaAction parameter of the Merge method. This example creates two versions of the same table, modifying the schema for the second version. The code then attempts to merge the second table into the first.


This example shows how to use one of the overloaded versions of Merge. For other examples that might be available, see the individual overload topics.

private static void DemonstrateMergeTable()
    DataTable table1 = new DataTable("Items");

    // Add columns
    DataColumn idColumn = new DataColumn("id", typeof(System.Int32));
    DataColumn itemColumn = new DataColumn("item", typeof(System.Int32));

    // Set the primary key column.
    table1.PrimaryKey = new DataColumn[] { idColumn };

    // Add RowChanged event handler for the table.
    table1.RowChanged += new 

    // Add ten rows.
    DataRow row;
    for (int i = 0; i <= 9; i++)
        row = table1.NewRow();
        row["id"] = i;
        row["item"] = i;

    // Accept changes.
    PrintValues(table1, "Original values");

    // Create a second DataTable identical to the first.
    DataTable table2 = table1.Clone();

    // Add column to the second column, so that the 
    // schemas no longer match.
    table2.Columns.Add("newColumn", typeof(System.String));

    // Add three rows. Note that the id column can't be the 
    // same as existing rows in the original table.
    row = table2.NewRow();
    row["id"] = 14;
    row["item"] = 774;
    row["newColumn"] = "new column 1";

    row = table2.NewRow();
    row["id"] = 12;
    row["item"] = 555;
    row["newColumn"] = "new column 2";

    row = table2.NewRow();
    row["id"] = 13;
    row["item"] = 665;
    row["newColumn"] = "new column 3";

    // Merge table2 into the table1.
    table1.Merge(table2, false, MissingSchemaAction.Add);
    PrintValues(table1, "Merged With table1, schema added");


private static void Row_Changed(object sender, 
    DataRowChangeEventArgs e)
    Console.WriteLine("Row changed {0}\t{1}", e.Action, 

private static void PrintValues(DataTable table, string label)
    // Display the values in the supplied DataTable:
    foreach (DataRow row in table.Rows)
        foreach (DataColumn col in table.Columns)
            Console.Write("\t " + row[col].ToString());
Return to top