Export (0) Print
Expand All

DataColumnCollection.CollectionChanged Event

Occurs when the columns collection changes, either by adding or removing a column.

[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

Use the Contains and CanRemove methods to determine if a column exists and can be removed.

Example

[Visual Basic, C#, C++] The following example adds an event handler for the CollectionChanged event.

[Visual Basic] 
Private Sub MyAddHandler(myTable As DataTable)
    Dim cols As DataColumnCollection = myTable.Columns
    AddHandler cols.CollectionChanged, AddressOf ColumnsCollection_Changed
End Sub    
   
Private Sub ColumnsCollection_Changed _
(sender As Object, e As System.ComponentModel.CollectionChangeEventArgs)
    Dim cols As DataColumnCollection = CType(sender, DataColumnCollection)
    Console.WriteLine("ColumnsCollectionChanged: " + cols.Count.ToString())
End Sub

[C#] 
private void AddHandler(DataTable myTable){
   DataColumnCollection cols = myTable.Columns;
   cols.CollectionChanged += new System.ComponentModel.CollectionChangeEventHandler(ColumnsCollection_Changed);
}

private void ColumnsCollection_Changed(object sender, System.ComponentModel.CollectionChangeEventArgs e){
   DataColumnCollection cols = (DataColumnCollection) sender;
   Console.WriteLine("ColumnsCollectionChanged: " + cols.Count);
}

[C++] 
private:
 void AddHandler(DataTable* myTable){
    DataColumnCollection* cols = myTable->Columns;
    cols->CollectionChanged += new System::ComponentModel::CollectionChangeEventHandler(this, &Form1::ColumnsCollection_Changed);
 }
 
 void ColumnsCollection_Changed(Object* sender, System::ComponentModel::CollectionChangeEventArgs* /*e*/){
    DataColumnCollection* cols = dynamic_cast<DataColumnCollection*> (sender);
    Console::WriteLine(S"ColumnsCollectionChanged: {0}", __box(cols->Count));
 }

[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

DataColumnCollection Class | DataColumnCollection Members | System.Data Namespace

Show:
© 2014 Microsoft