Export (0) Print
Expand All

DataTableCollection.CollectionChanging Event

Occurs while the DataTableCollection is changing as a result of DataTable objects being added or removed.

[Visual Basic]
Public Event CollectionChanging As CollectionChangeEventHandler
[C#]
public event CollectionChangeEventHandler CollectionChanging;
[C++]
public: __event CollectionChangeEventHandler* CollectionChanging;

[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.

Remarks

For more information about handling events, see Consuming Events.

Example

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

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

    AddHandler myDataSet.Tables.CollectionChanging, AddressOf Collection_Changing

    ' 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")}

    ' Add the tables to the DataTableCollection
    myDataSet.Tables.AddRange(New DataTable() {t, t2})

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

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

Private Sub Collection_Changing(ByVal sender As Object, ByVal e As System.ComponentModel.CollectionChangeEventArgs)
    ' Implementing this event allows you to abort a change
    ' to the collection by raising an exception which you can
    ' catch.
    Console.WriteLine("Collection_Changing Event: '{0}'\t element={1}", _
        e.Action.ToString(), e.Element.ToString())
End Sub

[C#] 
public static void TableCollectionCollectionChanging()
{
    // Create a DataSet with two tables
    DataSet myDataSet = new DataSet();

    // Assign the event-handler function for the CollectionChangeEvent.
    myDataSet.Tables.CollectionChanging += 
        new System.ComponentModel.CollectionChangeEventHandler( Collection_Changing );

    // 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} );

    // Check to see if each table can be removed and then
    // remove it.
    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_Changing(object sender, System.ComponentModel.CollectionChangeEventArgs e)
{
    // Implementing this event allows you to abort a change
        // to the collection by raising an exception which you can
        // catch.

    Console.WriteLine( "Collection_Changing Event: '{0}'\t element={1}", 
        e.Action.ToString(), e.Element.ToString() );
}

[C++] 
public:
   static void TableCollectionCollectionChanging()
   {
      // Create a DataSet with two tables
      DataSet* myDataSet = new DataSet();

      // Assign the event-handler function for the CollectionChangeEvent.
      myDataSet->Tables->CollectionChanging += 
         new System::ComponentModel::CollectionChangeEventHandler(0,  Collection_Changing );

      // 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* temp1 [] = {t->Columns->Item[S"customerId"]};
      t->PrimaryKey = temp1;

      // 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* temp2 [] = {t->Columns->Item[S"orderId"]};
      t2->PrimaryKey = temp2;

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

      // Check to see if each table can be removed and then
      // remove it.
      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));
   }

private:
   static void Collection_Changing(Object* /*sender*/, System::ComponentModel::CollectionChangeEventArgs* e)
   {
      // Implementing this event allows you to abort a change
      // to the collection by raising an exception which you can
      // catch.

      Console::WriteLine( S"Collection_Changing 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.

Requirements

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

Show:
© 2015 Microsoft