CollectionChanged Event
Collapse the table of content
Expand the table of content

DataTableCollection.CollectionChanged Event


Occurs after the DataTableCollection is changed because of DataTable objects being added or removed.

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

public event CollectionChangeEventHandler CollectionChanged

For more information about handling events, see NIB: Consuming Events.

The following example demonstrates how to use the CollectionChanged event.

public static void TableCollectionCollectionChanged()
    // create a DataSet with two tables
    DataSet dataSet = new DataSet();

    dataSet.Tables.CollectionChanged +=
        new System.ComponentModel.CollectionChangeEventHandler(

    // create Customer table
    DataTable custTable = new DataTable("Customers");
        typeof(int)).AutoIncrement = true;
    custTable.PrimaryKey = new DataColumn[] { custTable.Columns["customerId"] };

    // create Orders table
    DataTable orderTable = new DataTable("Orders");
        typeof(int)).AutoIncrement = true;
    orderTable.Columns.Add("customerId", typeof(int));
    orderTable.Columns.Add("amount", typeof(double));
    orderTable.PrimaryKey = new DataColumn[] { orderTable.Columns["orderId"] };

    dataSet.Tables.AddRange(new DataTable[] { custTable, orderTable });

    // remove all tables
    // check if table can be removed and then
    // remove it, cannot use a foreach when
    // removing items from a collection
    // equivalent to dataSet.Tables.Clear()
    while (dataSet.Tables.Count > 0)
        DataTable table = dataSet.Tables[0];
        if (dataSet.Tables.CanRemove(table))

    Console.WriteLine("dataSet has {0} tables",

private static void Collection_Changed(object sender,
    System.ComponentModel.CollectionChangeEventArgs e)
    Console.WriteLine("Collection_Changed Event: '{0}'\table element={1}",
        e.Action.ToString(), e.Element.ToString());

.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft