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 CollectionChanged As CollectionChangeEventHandler

For more information about handling events, see Consuming Events.

The following example demonstrates how to use the CollectionChanged event.

Public Sub TableCollectionCollectionChanged()
    ' create a DataSet with two tables 
    Dim dataSet As DataSet = New DataSet()

    AddHandler dataSet.Tables.CollectionChanging, _
        AddressOf Collection_Changed

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

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

    dataSet.Tables.AddRange(New DataTable() {custTable, orderTable})

    ' 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 (dataSet.Tables.Count > 0)
        Dim table As DataTable = dataSet.Tables(0)
        If (dataSet.Tables.CanRemove(table)) Then
        End If 

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

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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

© 2014 Microsoft