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 Sub DataColumnChangeEventHandler(ByVal senderAs Object,ByVal e As DataColumnChangeEventArgs)Public Delegate Sub DataColumnChangeEventHandler(ByVal senderAs Object,ByVal e As DataColumnChangeEventArgs)
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 Sub ColumnChange_Click(ByVal sender As Object,ByVal e As System.EventArgs)
Dim custTableAs New DataTable("Customers")
' add columns
custTable.Columns.Add("id",GetType(Integer))
custTable.Columns.Add("name",GetType(String))
custTable.Columns.Add("address",GetType(String))
' Add ColumnChanging and ColumnChanged event handlers
AddHandler custTable.ColumnChanging, AddressOf Column_Changing
AddHandler custTable.ColumnChanged,AddressOf 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
For Each row As DataRowIn custTable.Rows
row("name") ="new name"
Next
End Sub
Private Shared Sub Column_Changed(ByVal senderAs Object,ByVal e As DataColumnChangeEventArgs)
MessageBox.Show(((("Column_changed Event: " & " , ") + e.Row("name") & " ,") + e.Column.ColumnName &", ") + e.Row("name", DataRowVersion.Original))
End Sub
Private Shared Sub Column_Changing(ByVal senderAs Object,ByVal e As DataColumnChangeEventArgs)
MessageBox.Show(((("Column_changing Event: " & " , ") + e.Row("name") & " ,") + e.Column.ColumnName &", ") + e.Row("name", DataRowVersion.Original))
End Sub
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 Sub UpdateRow_Click(ByVal sender As Object,ByVal e As System.EventArgs)
Dim custTableAs New DataTable("Customers")
' add columns
custTable.Columns.Add()
custTable.Columns.Add("id",GetType(Integer))
custTable.Columns.Add("name",GetType(String))
custTable.Columns.Add("address",GetType(String))
' add Two rows
custTable.Rows.Add(New Object() {1, "name1", "address1"})
custTable.Rows.Add(New Object() {2, "name2", "address2"})
custTable.AcceptChanges()
For Each row As DataRow In custTable.Rows
row("name") ="new name"
' Adding RowChanged and RowChanging event handlers
AddHandler custTable.RowChanged, AddressOf Row_Changed
AddHandler custTable.RowChanging,AddressOf Row_Changing
Next
End Sub
Private Shared Sub Row_Changed(ByVal senderAs Object,ByVal e As DataRowChangeEventArgs)
MessageBox.Show(("Row_Changed Event:" & e.Row("name", DataRowVersion.Original).ToString()) + e.Action.ToString())
End Sub
Private Shared Sub Row_Changing(ByVal senderAs Object,ByVal e As DataRowChangeEventArgs)
MessageBox.Show(("Row_Changing Event:" & e.Row("name", DataRowVersion.Original).ToString()) + e.Action.ToString())
End Sub
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 Sub DeleteRow_Click(ByVal sender As Object,ByVal e As System.EventArgs)
Dim custTableAs New DataTable("Customers")
' add columns
custTable.Columns.Add()
custTable.Columns.Add("id",GetType(Integer))
custTable.Columns.Add("name",GetType(String))
custTable.Columns.Add("address",GetType(String))
' Add RowDeleting and RowDeleted events
AddHandler custTable.RowDeleting, AddressOf Row_Deleting
AddHandler custTable.RowDeleted,AddressOf 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
For Each row As DataRowIn custTable.Rows
row.Delete()
Next
End Sub
Private Shared Sub Row_Deleting(ByVal senderAs Object,ByVal e As DataRowChangeEventArgs)
MessageBox.Show(("Row_ Deleting Event:" & e.Row("name", DataRowVersion.Original).ToString()) + e.Action.ToString())
End Sub
Private Shared Sub Row_Deleted(ByVal senderAs Object,ByVal e As DataRowChangeEventArgs)
MessageBox.Show(("Row_Deleted Event:" & e.Row("name", DataRowVersion.Original).ToString()) + e.Action.ToString())
End Sub
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.