A DataTable represents a table of a DataSet. A DataTable provides many events that can be tracked down by an application (see Table 5-5).
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 value of a row in the table is changed. |
RowChanging | This event occurs when a row in a table is changing. |
RowDeleted | This event occurs when a row in a table is deleted. |
RowDeleting | This event occurs when a row is being deleted. |
The ColumnChangedEventHandler handles the ColumnChanged event and is defined as follows:
Public Delegate Sub DataColumnChangeEventHandler(_ ByVal sender As Object, _ ByVal e As DataColumnChangeEventArgs _ )
In this example, sender is the source of the event and e is DataColumnChangedEventArgs that contains the event data.
ColumnChangingEventHandler handles the ColumnChanging event and is defined as follows:
Public Delegate Sub DataColumnChangeEventHandler(_ ByVal sender As Object, _ ByVal e As DataColumnChangeEventArgs _ )
In this example, sender is the source of the event and e is DataColumnChangingEventArgs that contains the event data.
Similar to these two event handlers, RowChangedEventHandler, RowChangingEventHandler, RowDeletedEventHandler, and RowDeletedEventHandler handle the RowChanged, RowChanging, RowDeleted, and RowDeleting events, respectively. Definitions of these event handlers are similar to DataColumnChangingEventHandler and DataColumnChangedEventHandler—except the EventArgs class is different.
To test these events you'll create a DataTable, add DataRows to it, and then update and delete rows from the table.
Listing 5-9 creates a DataTable, adds three columns—id, name, and address—adds data to the table and changes the column 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. You can write this code on a button click event handler.
Listing 5-9: Writing the ColumnChanging and ColumnChanged Event Handlers
Private Sub ColumnChange_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ColumnChange.Click 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")) ' add a ColumnChanged & ColumnChanging event handler for the table. AddHandler custTable.ColumnChanged, New DataColumnChangeEventHandler _ (AddressOf Column_Changed) AddHandler custTable.ColumnChanging, New DataColumnChangeEventHandler _ (AddressOf Column_Changing) ' add Two rows custTable.Rows.Add(New Object() {1, "name1", "address1"}) custTable.Rows.Add(New Object() {2, "name2", "address2"}) ' Save changes custTable.AcceptChanges() ' change Address column in all the rows Dim row As DataRow For Each row In custTable.Rows row("address") = "New address" Next row ' Removing event handlers RemoveHandler custTable.ColumnChanged, AddressOf Column_Changed RemoveHandler custTable.ColumnChanging, AddressOf Column_Changing End Sub ' Column changed event handler Private Shared Sub Column_Changed(ByVal sender As Object, _ ByVal e As DataColumnChangeEventArgs) MessageBox.Show("Column_Changed Event: " + " ," & _ e.Row("name") + " ," + e.Column.ColumnName + " ," & _ e.Row("name", DataRowVersion.Original)) End Sub ' Column Changing event handler Private Shared Sub Column_Changing(ByVal sender As Object, _ ByVal e As DataColumnChangeEventArgs) MessageBox.Show("Column_Changing Event: " + " ," & _ e.Row("name") + " ," + e.Column.ColumnName + " ," & _ e.Row("name", DataRowVersion.Original)) End Sub
Listing 5-10 creates a DataTable, adds three columns—id, name, and address—adds data to the table, and changes the column of the table. It also calls the RowChanging and RowChanged event handlers.
Listing 5-10: Writing the RowChanging and RowChanged Event Handlers
' Row Changed and Row Changing Events Caller Private Sub UpdateRow_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles UpdateRow.Click 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")) ' add a ColumnChanged & ColumnChanging event handler for the table. AddHandler custTable.RowChanged, New DataRowChangeEventHandler _ (AddressOf Row_Changed) AddHandler custTable.RowChanged, New DataRowChangeEventHandler _ (AddressOf Row_Changing) ' add Two rows custTable.Rows.Add(New Object() {1, "name1", "address1"}) custTable.Rows.Add(New Object() {2, "name2", "address2"}) ' Save changes custTable.AcceptChanges() ' change Address column in all the rows Dim row As DataRow For Each row In custTable.Rows row("name") = "New name" Next row ' Removing event handlers RemoveHandler custTable.RowChanged, AddressOf Row_Changed RemoveHandler custTable.RowChanging, AddressOf Row_Changing End Sub ' Row Changed event handler Private Shared Sub Row_Changed(ByVal sender As Object, _ ByVal e As DataRowChangeEventArgs) MessageBox.Show("Row_Changed Event:" & _ e.Row("name", DataRowVersion.Original).ToString() & _ e.Action.ToString()) End Sub ' Row Changing event handler Private Shared Sub Row_Changing(ByVal sender As Object, _ ByVal e As DataRowChangeEventArgs) MessageBox.Show("Row_Changing Event:" & _ e.Row("name", DataRowVersion.Original).ToString() & _ e.Action.ToString()) End Sub
Listing 5-11 creates a DataTable, adds three columns—id, name, and address—adds data to the table, and changes a column of the table. It also calls the RowDeleting and RowDeleted event handlers.
Listing 5-11: Writing the RowDeleting and RowDeleted Event Handlers
' Row Deleted and Row Deleting Events Caller Private Sub DeleteRow_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles DeleteRow.Click 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")) ' add a ColumnChanged & ColumnChanging event handler for the table. AddHandler custTable.RowDeleted, New DataRowChangeEventHandler _ (AddressOf Row_Deleted) AddHandler custTable.RowDeleting, New DataRowChangeEventHandler _ (AddressOf Row_Deleting) ' add Two rows custTable.Rows.Add(New Object() {1, "name1", "address1"}) custTable.Rows.Add(New Object() {2, "name2", "address2"}) ' Save changes custTable.AcceptChanges() ' change Address column in all the rows Dim row As DataRow ' Go through all rows For Each row In custTable.Rows ' Delete the row row.Delete() Next row ' Removing event handlers RemoveHandler custTable.RowDeleted, AddressOf Row_Deleted RemoveHandler custTable.RowDeleting, AddressOf Row_Deleting End Sub ' Row Deleted event handler Private Shared Sub Row_Deleted(ByVal sender As Object, _ ByVal e As DataRowChangeEventArgs) MessageBox.Show("Row_Deleted Event:" & _ e.Row("name", DataRowVersion.Original).ToString() & _ e.Action.ToString()) End Sub 'Row Deleting event handler Private Shared Sub Row_Deleting(ByVal sender As Object, _ ByVal e As DataRowChangeEventArgs) MessageBox.Show("Row_Deleting Event:" & _ e.Row("name", DataRowVersion.Original).ToString() & _ e.Action.ToString()) End Sub