This article has been excerpted from the book "A Programmer's Guide to ADO.NET in C#".
A DataTable represents a table of a dataset. DataTable provides many events that an application can track down (see Table 9-5).
Table 9-5. The DataTable Events
|
EVENT
|
DESCRIPTION
|
|
ColumnChanged
|
This event occurs when the value of a column has been changed.
|
|
ColumnChanging
|
This event occurs when a new value is being added to a column.
|
|
RowChanged
|
This event occurs when the value of a row in the table has been changed.
|
|
RowChanging
|
This event occurs when a row in a table has been changed.
|
|
RowDeleted
|
This event occurs when a row in a table has been deleted.
|
|
RowDeleting
|
This event occurs when a row is being deleted.
|
ColumnChangedEventHandler handles the ColumnChanged event; it's as follows:
- public delegate void DataColumnChangeEventHandler(object sender, DataColumnChangeEventArgs e);
Where sender is the source of the event and e is DataColumnChangedEventArgs, which contains the event data.
ColumnChangingEventHandler handles the ColumnChanging Event; it's as follows:
- public delegate void DataColumnChangeEventHandler(object sender,DataColumnChangeEventArgs e);
Where Sender is the source of the event and e is DataColumnChangingEventArgs, which contains the event data.
Similarly, to these two handlers, RowChangedEventHandler, RowChangingEventHandler, RowDeletingEventHandler, and RowDeletedEventHandler handle the RowChanged, RowChanging, RowDeleting, and RowDeleted events, respectively. Definitions of these event handlers are similar to DataColumnChangingEventHandler and DataColumnChangedEventHandler.
To test these I'll create a data table, add data rows to the table, and then update and delete rows from the table.
Listing 9-8 creates a data table, adds three columns (id, name, and address), adds data to the table, and changes the columns of the table. It also calls the ColumnChanged and ColumnChanging event handlers. You write the code for the ColumnChanged and ColumnChanging event handlers in the Column_Changed and Column_Changing methods. Specifically, you can write this code on a button-click event handler.
Listing 9-8. Writing the Column and ColumnChanged event handlers
- private void ColumnChange_Click(object sender, System.EventArgs e) {
- 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));
- // Add ColumnChanging and ColumnChanged event handlers
- custTable.ColumnChanging += new DataColumnChangeEventHandler(Column_Changing);
- custTable.ColumnChanged += new DataColumnChangeEventHandler(Column_Changed);
- // add Two rows
- custTable.Rows.Add(new object[] { 1, "name1", "address1" });
- custTable.Rows.Add(new object[] { 2, "name2", "address2" });
- custTable.AcceptChanges();
- // Change the name column in all the rows
- foreach(DataRow row in custTable.Rows) {
- row["name"] = "new name";
- }
- }
- private static void Column_Changed(object sender, DataColumnChangeEventArgs e) {
- MessageBox.Show("Column_changed Event: " + " , " +
- e.Row["name"] + " ," + e.Column.ColumnName + ", " +
- e.Row["name", DataRowVersion.Original]);
- }
- private static void Column_Changing(object sender, DataColumnChangeEventArgs e) {
- MessageBox.Show("Column_changing Event: " + " , " +
- e.Row["name"] + " ," + e.Column.ColumnName + ", " +
- e.Row["name", DataRowVersion.Original]);
- }

Join the conversation! Your thoughts help the community grow.