Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

DataTable.ColumnChanged Event

Occurs after a value has been changed for the specified DataColumn in a DataRow.

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

[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 DataColumnChangeEventArgs containing data related to this event. The following DataColumnChangeEventArgs properties provide information specific to this event.

Property Description
Column Gets the DataColumn with a changing value.
ProposedValue Gets or sets the proposed new value for the column.
Row Gets the DataRow of the column with a changing value.

Remarks

For more information see Working with DataTable Events.

Example

[Visual Basic] 
Private Shared Sub DataTableColumnChanged()
    Dim custTable As DataTable = New DataTable("Customers")
    ' add columns
    custTable.Columns.Add( "id", Type.GetType("System.Int32") )
    custTable.Columns.Add( "name", Type.GetType("System.String") )
    custTable.Columns.Add( "address", Type.GetType("System.String") )

    ' set PrimaryKey
    custTable.Columns( "id" ).Unique = true
    custTable.PrimaryKey = New DataColumn() { custTable.Columns("id") }

    ' add a ColumnChanged event handler for the table.
    AddHandler custTable.ColumnChanged, New DataColumnChangeEventHandler( AddressOf Column_Changed )


    ' add ten rows
    Dim id As Integer
    For id = 1 To 10
        custTable.Rows.Add( _
            New Object() { id, string.Format("customer{0}", id), string.Format("address{0}", id) } )
    Next
    
    custTable.AcceptChanges()

    ' change the name column in all the rows
    Dim row As DataRow
    For Each row In custTable.Rows 
        row("name") = string.Format( "vip{0}", row("id") )
    Next

End Sub

Private Shared Sub Column_Changed(sender As Object, e As DataColumnChangeEventArgs)
    Console.WriteLine( "Column_Changed Event: name={0}; Column={1}; original name={2}", _
        e.Row("name"), e.Column.ColumnName, e.Row("name", DataRowVersion.Original) )
End Sub

[C#] 
private static void DataTableColumnChanged()
{
    DataTable custTable = new DataTable("Customers");
    // add columns
    custTable.Columns.Add( "id", typeof(int) );
    custTable.Columns.Add( "name", typeof(string) );
    custTable.Columns.Add( "address", typeof(string) );

    // set PrimaryKey
    custTable.Columns[ "id" ].Unique = true;
    custTable.PrimaryKey = new DataColumn[] { custTable.Columns["id"] };

    // add a ColumnChanged event handler for the table.
    custTable.ColumnChanged += new DataColumnChangeEventHandler( Column_Changed );


    // add ten rows
    for( int id=1; id<=10; id++ )
    {
        custTable.Rows.Add( 
            new object[] { id, string.Format("customer{0}", id), string.Format("address{0}", id) } );
    }
    
    custTable.AcceptChanges();

    // change the name column in all the rows
    foreach( DataRow row in custTable.Rows )
    {
        row["name"] = string.Format( "vip{0}", row["id"] );
    }

}

private static void Column_Changed( object sender, DataColumnChangeEventArgs e )
{
    Console.WriteLine( "Column_Changed Event: name={0}; Column={1}; original name={2}", 
        e.Row["name"], e.Column.ColumnName, e.Row["name", DataRowVersion.Original] );
}

[C++] 

public:
    static void DataTableColumnChanged() {
        DataTable* custTable = new DataTable(S"Customers");
        // add columns
        custTable->Columns->Add(S"id", __typeof(int));
        custTable->Columns->Add(S"name", __typeof(String));
        custTable->Columns->Add(S"address", __typeof(String));

        // set PrimaryKey
        custTable->Columns->Item[ S"id" ]->Unique = true;
        DataColumn* c[] = { custTable->Columns->Item[S"id"] };
        custTable->PrimaryKey = c;

        // add a ColumnChanged event handler for the table.
        custTable->ColumnChanged += new DataColumnChangeEventHandler(0,Column_Changed);

        // add ten rows
        for (int id=1; id<=10; id++) {

            Object* myArray[] =    {
                __box(id), 
                String::Format(S"customer {0}", __box(id)), 
                String::Format(S"address {0}", __box(id)) 
            };

            custTable->Rows->Add( myArray );
        }

        custTable->AcceptChanges();

        // change the name column in all the rows
        IEnumerator* myEnum = custTable->Rows->GetEnumerator();
        while (myEnum->MoveNext()) {
            DataRow* row = __try_cast<DataRow*>(myEnum->Current);

            row->Item[S"name"] = String::Format(S"vip {0}", row->Item[S"id"]);
        }

    };

public:
    static void Column_Changed(Object* sender, DataColumnChangeEventArgs* e) {
        Console::WriteLine(S"Column_Changed Event: name= {0}; Column= {1}; original name= {2}", 
            e->Row->Item[S"name"], e->Column->ColumnName, e->Row->Item[S"name", DataRowVersion::Original]);
    };

[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

DataTable Class | DataTable Members | System.Data Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.