This documentation is archived and is not being maintained.

DataTableCollection.CollectionChanged Event

Occurs after the DataTableCollection is changed as a result of DataTable objects being added or removed.

[Visual Basic]
Public Event CollectionChanged As CollectionChangeEventHandler
public event CollectionChangeEventHandler CollectionChanged;
public: __event CollectionChangeEventHandler* CollectionChanged;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type CollectionChangeEventArgs containing data related to this event. The following CollectionChangeEventArgs properties provide information specific to this event.

Property Description
Action Gets an action that specifies how the collection changed.
Element Gets the instance of the collection with the change.


For more information about handling events, see Consuming Events.


[Visual Basic, C#, C++] The following example demonstrates how to use the CollectionChanged event.

[Visual Basic] 
Public Sub TableCollectionCollectionChanged()
    ' create a DataSet with two tables
    Dim myDataSet As DataSet = New DataSet()

    AddHandler myDataSet.Tables.CollectionChanging, AddressOf Collection_Changed

    ' create Customer table
    Dim t As DataTable = New DataTable("Customers")
    t.Columns.Add("customerId", System.Type.GetType("System.Integer")).AutoIncrement = True
    t.Columns.Add("name", System.Type.GetType("System.String"))
    t.PrimaryKey = New DataColumn() {t.Columns("customerId")}

    ' create Orders table
    Dim t2 As DataTable = New DataTable("Orders")
    t2.Columns.Add("orderId", System.Type.GetType("System.Integer")).AutoIncrement = True
    t2.Columns.Add("customerId", System.Type.GetType("System.Integer"))
    t2.Columns.Add("amount", System.Type.GetType("System.Double"))
    t2.PrimaryKey = New DataColumn() {t.Columns("orderId")}

    myDataSet.Tables.AddRange(New DataTable() {t, t2})

    ' Now remove all tables.
    ' First check to see if the table can be removed,
    ' then remove it from the collection.
    ' You cannot use a For Each loop when
    ' removing items from a collection.
    Do While (myDataSet.Tables.Count > 0)
        Dim table As DataTable = myDataSet.Tables(0)
        If (myDataSet.Tables.CanRemove(table)) Then
        End If

    Console.WriteLine("myDataSet has {0} tables", myDataSet.Tables.Count)
End Sub

Private Sub Collection_Changed(ByVal sender As Object, ByVal e As System.ComponentModel.CollectionChangeEventArgs)
    Console.WriteLine("Collection_Changed Event: '{0}'\t element={1}", _
        e.Action.ToString(), e.Element.ToString())
End Sub

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

    myDataSet.Tables.CollectionChanged += 
        new System.ComponentModel.CollectionChangeEventHandler( Collection_Changed );

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

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

    myDataSet.Tables.AddRange( new DataTable[] {t, t2} );

    // 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 myDataSet.Tables.Clear()
    while( myDataSet.Tables.Count > 0 )
        DataTable table = myDataSet.Tables[0];
        if( myDataSet.Tables.CanRemove( table ) )
            myDataSet.Tables.RemoveAt( 0 );

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

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

   static void TableCollectionCollectionChanged()
      // create a DataSet with two tables
      DataSet* myDataSet = new DataSet();

      myDataSet->Tables->CollectionChanged += 
         new System::ComponentModel::CollectionChangeEventHandler(0,  Collection_Changed );

      // create Customer table
      DataTable* t = new DataTable( S"Customers" );
      t->Columns->Add( S"customerId", __typeof(int)    )->AutoIncrement = true;
      t->Columns->Add( S"name",       __typeof(String) );

      DataColumn* temp0 [] = {t->Columns->Item[S"customerId"]};
      t->PrimaryKey = temp0;

      // create Orders table
      DataTable* t2 = new DataTable( S"Orders" );
      t2->Columns->Add( S"orderId",    __typeof(int)    )->AutoIncrement = true;
      t2->Columns->Add( S"customerId", __typeof(int)    );
      t2->Columns->Add( S"amount",     __typeof(double) );

      DataColumn* temp1 [] = {t->Columns->Item[S"orderId"]};
      t2->PrimaryKey = temp1;

      DataTable* temp2 [] = {t, t2};
      myDataSet->Tables->AddRange( temp2 );

      // 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 myDataSet.Tables.Clear()
      while( myDataSet->Tables->Count > 0 )
         DataTable* table = myDataSet->Tables->Item[0];
         if( myDataSet->Tables->CanRemove( table ) )
            myDataSet->Tables->RemoveAt( 0 );

      Console::WriteLine( S"myDataSet has {0} tables", __box(myDataSet->Tables->Count));

   static void Collection_Changed(Object* /*sender*/, System::ComponentModel::CollectionChangeEventArgs* e)
      Console::WriteLine( S"Collection_Changed Event: '{0}'\t element={1}", __box(e->Action), e->Element );

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

DataTableCollection Class | DataTableCollection Members | System.Data Namespace