Working with DataTable Events in ADO.NET

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:
  1. 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:
  1. 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
  1. private void ColumnChange_Click(object sender, System.EventArgs e) {  
  2.     DataTable custTable = new DataTable("Customers");  
  3.     // add columns  
  4.     custTable.Columns.Add("id"typeof(int));  
  5.     custTable.Columns.Add("name"typeof(string));  
  6.     custTable.Columns.Add("address"typeof(string));  
  7.     // Add ColumnChanging and ColumnChanged event handlers  
  8.     custTable.ColumnChanging += new DataColumnChangeEventHandler(Column_Changing);  
  9.     custTable.ColumnChanged += new DataColumnChangeEventHandler(Column_Changed);  
  10.     // add Two rows  
  11.     custTable.Rows.Add(new object[] { 1, "name1""address1" });  
  12.     custTable.Rows.Add(new object[] { 2, "name2""address2" });  
  13.     custTable.AcceptChanges();  
  14.     // Change the name column in all the rows  
  15.     foreach(DataRow row in custTable.Rows) {  
  16.         row["name"] = "new name";  
  17.     }  
  18. }  
  19. private static void Column_Changed(object sender, DataColumnChangeEventArgs e) {  
  20.     MessageBox.Show("Column_changed Event: " + " , " +  
  21.         e.Row["name"] + " ," + e.Column.ColumnName + ", " +  
  22.         e.Row["name", DataRowVersion.Original]);  
  23. }  
  24. private static void Column_Changing(object sender, DataColumnChangeEventArgs e) {  
  25.     MessageBox.Show("Column_changing Event: " + " , " +  
  26.         e.Row["name"] + " ," + e.Column.ColumnName + ", " +  
  27.         e.Row["name", DataRowVersion.Original]);  
Listing 9-9 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 RowChanging and RowChanged event handlers.
 
Listing 9-9. Writing the RowChanging and RowChanged event handlers
  1. private void UpdateRow_Click(object sender, System.EventArgs e) {  
  2.     DataTable custTable = new DataTable("Customers");  
  3.     // add columns  
  4.     custTable.Columns.Add();  
  5.     custTable.Columns.Add("id"typeof(int));  
  6.     custTable.Columns.Add("name"typeof(string));  
  7.     custTable.Columns.Add("address"typeof(string));  
  8.     // add Two rows  
  9.     custTable.Rows.Add(new object[] { 1, "name1""address1" });  
  10.     custTable.Rows.Add(new object[] { 2, "name2""address2" });  
  11.     custTable.AcceptChanges();  
  12.     foreach(DataRow row in custTable.Rows) {  
  13.         row["name"] = "new name";  
  14.         // Adding RowChanged and RowChanging event handlers  
  15.         custTable.RowChanged += new DataRowChangeEventHandler(Row_Changed);  
  16.         custTable.RowChanging += new DataRowChangeEventHandler(Row_Changing);  
  17.     }  
  18. }  
  19. private static void Row_Changed(object sender, DataRowChangeEventArgs e) {  
  20.     MessageBox.Show("Row_Changed Event:" +  
  21.         e.Row["name", DataRowVersion.Original].ToString() +  
  22.         e.Action.ToString());  
  23. }  
  24. private static void Row_Changing(object sender, DataRowChangeEventArgs e) {  
  25.     MessageBox.Show("Row_Changing Event:" +  
  26.         e.Row["name", DataRowVersion.Original].ToString() +  
  27.         e.Action.ToString());  
Listing 9-10 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 RowDeleting and RowDeleted event handlers.
 
Listing 9-10. Writing the RowDeleting and RowDeleted event handlers
  1. private void DeleteRow_Click(object sender, System.EventArgs e) {  
  2.     DataTable custTable = new DataTable("Customers");  
  3.     // add columns  
  4.     custTable.Columns.Add();  
  5.     custTable.Columns.Add("id"typeof(int));  
  6.     custTable.Columns.Add("name"typeof(string));  
  7.     custTable.Columns.Add("address"typeof(string));  
  8.     // Add RowDeleting and RowDeleted events  
  9.     custTable.RowDeleting += new DataRowChangeEventHandler(Row_Deleting);  
  10.     custTable.RowDeleted += new DataRowChangeEventHandler(Row_Deleted);  
  11.     // add Two rows  
  12.     custTable.Rows.Add(new object[] { 1, "name1""address1" });  
  13.     custTable.Rows.Add(new object[] { 2, "name2""address2" });  
  14.     custTable.AcceptChanges();  
  15.     //Delete all the rows  
  16.     foreach(DataRow row in custTable.Rows)  
  17.     row.Delete();  
  18. }  
  19. private static void Row_Deleting(object sender, DataRowChangeEventArgs e) {  
  20.     MessageBox.Show("Row_ Deleting Event:" + e.Row["name", DataRowVersion.Original].ToString() + e.Action.ToString());  
  21. }  
  22. private static void Row_Deleted(object sender, DataRowChangeEventArgs e) {  
  23.     MessageBox.Show("Row_Deleted Event:" + e.Row["name", DataRowVersion.Original].ToString() + e.Action.ToString());  

Conclusion

 
Hope this article would have helped you in understanding working with DataTable Events in ADO.NET. See other articles on the website also for further reference.
 
adobook.jpg
 
This essential guide to Microsoft's ADO.NET overviews C# then leads you toward a deeper understanding of ADO.NET.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.