Working with DataTable Events in ADO.NET

This article has been excerpted from 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 a 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 a 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]);
        }

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

        private void UpdateRow_Click(object sender, System.EventArgs e)
        {
            DataTable custTable = new DataTable("Customers");

             // add columns
             custTable.Columns.Add();
             custTable.Columns.Add("id", typeof(int));
             custTable.Columns.Add("name", typeof(string));
             custTable.Columns.Add("address", typeof(string));

             // add Two rows
             custTable.Rows.Add(new object[] { 1, "name1", "address1" });
             custTable.Rows.Add( new object[] { 2, "name2", "address2" });
             custTable.AcceptChanges();

            foreach (DataRow row in custTable.Rows)
            {
                row["name"] = "new name";

                 // Adding RowChanged and RowChanging event handlers
                 custTable.RowChanged += new DataRowChangeEventHandler(Row_Changed);
                 custTable.RowChanging += new DataRowChangeEventHandler(Row_Changing);
            }
        }

        private static void Row_Changed (object sender, DataRowChangeEventArgs e)
        {
            MessageBox.Show("Row_Changed Event:" +
            e.Row["name", DataRowVersion.Original].ToString() +
            e.Action.ToString());
        }

        private static void Row_Changing(object sender, DataRowChangeEventArgs e)
        {
            MessageBox.Show("Row_Changing Event:" +
            e.Row["name", DataRowVersion.Original].ToString() +
            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

        private void DeleteRow_Click(object sender, System.EventArgs e)
        {
            DataTable custTable = new DataTable("Customers");

             // add columns
             custTable.Columns.Add();
             custTable.Columns.Add("id", typeof(int));
             custTable.Columns.Add("name", typeof(string));
             custTable.Columns.Add("address", typeof(string));

             // Add RowDeleting and RowDeleted events
             custTable.RowDeleting += new DataRowChangeEventHandler(Row_Deleting);
             custTable.RowDeleted += new DataRowChangeEventHandler(Row_Deleted);

             // add Two rows
             custTable.Rows.Add(new object[] { 1, "name1", "address1" });
             custTable.Rows.Add(new object[] { 2, "name2", "address2" });
             custTable.AcceptChanges();

             //Delete all the rows
            foreach (DataRow row in custTable.Rows)
                row.Delete();
        }

        private static void Row_Deleting(object sender, DataRowChangeEventArgs e)
        {
            MessageBox.Show("Row_ Deleting Event:" +e.Row["name", DataRowVersion.Original].ToString() +e.Action.ToString());
        }

        private static void Row_Deleted (object sender, DataRowChangeEventArgs e)
        {
            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 deeper understanding of ADO.NET.