Export (0) Print
Expand All

ConstraintCollection.CollectionChanged Event

Occurs whenever the ConstraintCollection is changed as a result of Constraint objects being added or removed.

[Visual Basic]
Public Event CollectionChanged As CollectionChangeEventHandler
[C#]
public event CollectionChangeEventHandler CollectionChanged;
[C++]
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.

Remarks

For more information about handling events, see Consuming Events.

Example

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

[Visual Basic] 
Private Shared Sub ConstraintCollectionChanged()

    ' Demonstrate ConstraintCollection.CollectionChanged event.

    Try
        ' Create Customers table.
        Dim tCustomers As DataTable = New DataTable( "Customers" )
        tCustomers.Columns.Add( "id", Type.GetType("System.Int32") )
        tCustomers.Columns.Add( "Name", Type.GetType("System.String") )
        AddHandler tCustomers.Constraints.CollectionChanged, _ 
            New System.ComponentModel.CollectionChangeEventHandler( AddressOf Collection_Changed )

        ' Create Orders table.
        Dim tOrders As DataTable = New DataTable( "Orders" )
        tOrders.Columns.Add( "CustID", Type.GetType("System.Int32") )
        tOrders.Columns.Add( "CustName", Type.GetType("System.String") )
        AddHandler tOrders.Constraints.CollectionChanged, _
            New System.ComponentModel.CollectionChangeEventHandler( AddressOf Collection_Changed )

        ' Create unique constraint.
        Dim uc As UniqueConstraint = New UniqueConstraint(tCustomers.Columns("id"))
        tCustomers.Constraints.Add(uc)
        
        ' Create unique constraint and specify as primary key.
        tOrders.Constraints.Add("pKey", tOrders.Columns("CustID"), True )

        ' Remove constraints.
        tCustomers.Constraints.RemoveAt( 0 )

        ' Results in an Exception. You can't remove a primary key constraint.
        tOrders.Constraints.RemoveAt( 0 )  
    Catch e As Exception
        ' Process exception and return.
                Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
                log.Source = "My Application"
                log.WriteEntry(e.ToString())
                Console.WriteLine("Exception of type {0} occurred.", e.GetType().ToString())
    End Try
End Sub

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

[C#] 

private static void ConstraintCollectionChanged()
{
    // Demonstrate ConstraintCollection.CollectionChanged event.

    try
    {
        // Create Customers table.
        DataTable tCustomers = new DataTable( "Customers" );
        tCustomers.Columns.Add( "id", typeof(int) );
        tCustomers.Columns.Add( "Name", typeof(string) );
        tCustomers.Constraints.CollectionChanged += 
            new System.ComponentModel.CollectionChangeEventHandler( Collection_Changed );

        // Create Orders table.
        DataTable tOrders = new DataTable( "Orders" );
        tOrders.Columns.Add( "CustID", typeof(int) );
        tOrders.Columns.Add( "CustName", typeof(string) );
        tOrders.Constraints.CollectionChanged += 
            new System.ComponentModel.CollectionChangeEventHandler( Collection_Changed );

        // Create unique constraint.
        UniqueConstraint uc = new UniqueConstraint(tCustomers.Columns["id"]);
        tCustomers.Constraints.Add(uc);
        
        // Create unique constraint and specify as primary key.
        tOrders.Constraints.Add("pKey", tOrders.Columns["CustID"], true );

        // Remove constraints.
        tCustomers.Constraints.RemoveAt( 0 );

        // Results in an Exception. You can't remove a primary key constraint.
        tOrders.Constraints.RemoveAt( 0 );  
    }
    catch(Exception e)
    {
        // Process exception and return.
                System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
                log.Source = "My Application";
                log.WriteEntry(e.ToString());
                Console.WriteLine("Exception of type {0} occurred.", e.GetType());
    }
}

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


[C++] 

public:
    static void ConstraintCollectionChanged() {
        // Demonstrate ConstraintCollection::CollectionChanged event.

        try {
            // Create Customers table.
            DataTable* tCustomers = new DataTable(S"Customers");
            tCustomers->Columns->Add(S"id", __typeof(int));
            tCustomers->Columns->Add(S"Name", __typeof(String));
            tCustomers->Constraints->CollectionChanged += new System::ComponentModel::CollectionChangeEventHandler(0, Collection_Changed);

            // Create Orders table.
            DataTable* tOrders = new DataTable(S"Orders");
            tOrders->Columns->Add(S"CustID", __typeof(int));
            tOrders->Columns->Add(S"CustName", __typeof(String));
            tOrders->Constraints->CollectionChanged += new System::ComponentModel::CollectionChangeEventHandler(0, Collection_Changed);

            // Create unique constraint.
            UniqueConstraint* uc = new UniqueConstraint(tCustomers->Columns->Item[S"id"]);
            tCustomers->Constraints->Add(uc);

            // Create unique constraint and specify as primary key.
            tOrders->Constraints->Add(S"pKey", tOrders->Columns->Item[S"CustID"], true);

            // Remove constraints.
            tCustomers->Constraints->RemoveAt(0);

            // Results in an Exception. You can't remove a primary key constraint.
            tOrders->Constraints->RemoveAt(0);  
        } catch (Exception* e) {
            // Process exception and return.
            System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
            log->Source = S"My Application";
            log->WriteEntry(e->ToString());
            Console::WriteLine(S"Exception of type {0} occurred.", e->GetType());
        }
    }

private:
    static void Collection_Changed(Object* /*sender*/, System::ComponentModel::CollectionChangeEventArgs* e) {
        Console::WriteLine(S"List_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.

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

ConstraintCollection Class | ConstraintCollection Members | System.Data Namespace

Show:
© 2015 Microsoft