Export (0) Print
Expand All

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
/** @event */
public void add_CollectionChanged (CollectionChangeEventHandler value)

/** @event */
public void remove_CollectionChanged (CollectionChangeEventHandler value)

JScript supports the use of events, but not the declaration of new ones.

For more information about handling events, see 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(
        Collection_Changed);

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

    // create Orders table
    DataTable orderTable = new DataTable("Orders");
    orderTable.Columns.Add("orderId",
        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))
        {
            dataSet.Tables.RemoveAt(0);
        
    

    Console.WriteLine("dataSet has {0 tables",
        dataSet.Tables.Count);


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());


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft