Working with DataView and DataViewManager Events in ADO.NET

This article has been excerpted from the book "A Programmer's Guide to ADO.NET in C#".
 
DataView and DataViewManager define the ListChanged event, which occurs when a row is added to or deleted from a DataView and DataViewManager object. The ListChangedEventHandler method handles the ListChanged event; it's as follows:
 
public delegate void ListChangedEventHandler(object sender, ListChangedEventArges e);
 
Where the sender is the source of the event and e is ListChangedEventArgs, which contains the event data. Table 9-8 defines ListChangedEventArgs members.
 
Table 9-8. The ListChangedEventArgs members
 
MEMBER DESCRIPTION
ListChangedType
Returns the way that lists changed
NewIndex
Returns the new index of the item in the list
OldIndex
Returns the old index of the item in the list
 
Listing 9-15 shows the OnListChanged event handler.
 
Listing 9-15.The OnListChanged event handler
  1. protected static void OnListChanged(object sender, System.ComponentModel.ListChangedEventArgs args) {  
  2.     MessageBox.Show("ListChanged: Type = " + args.ListChangedType +  
  3.           ", OldIndex = " + args.OldIndex +  
  4.           ", NewIndex = " + args.NewIndex);  
To test this application, you can create a Windows application and write the code in Listing 9-16 on the form load or a button-click event handler. As you can see from Listing 9-16, the code creates a DataView object, adds a new row to DataView, and then removes the first row from DataView. The adding and removing of rows is responsible for firing the OnListChanged event handler.
 
Listing 9-16. Adding, updating, and deleting rows of a DataView
  1. private void Form1_load(object sender, System.EventArgs e) {  
  2.     OleDbConnection conn = new OleDbConnection();  
  3.     string strDSN = "provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source= C:/Northwind.mdb";  
  4.     conn.ConnectionString = strDSN;  
  5.     string sql = "SELECT EmployeeID, LastName, FirstName FROM Employees";  
  6.     // Opening Connection  
  7.     conn.Open();  
  8.     // Create a data Adapter  
  9.     OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);  
  10.     // Create and fill DataSet  
  11.     DataSet ds = new DataSet();  
  12.     da.Fill(ds, "Employees");  
  13.     DataView dv = ds.Tables["Employees"].DefaultView;  
  14.     // Add DataView Event Handlers  
  15.     dv.ListChanged += new System.ComponentModel.ListChangedEventHandler(OnListChanged);  
  16.     // Add a row to the DataView  
  17.     dv.AllowEdit = true;  
  18.     DataRowView rw = dv.AddNew();  
  19.     rw.BeginEdit();  
  20.     rw["FirstName"] = "FName";  
  21.     rw["LastName"] = "LName";  
  22.     rw.EndEdit();  
  23.     // Remove a row from the DataView  
  24.     if (dv.Count > 0) {  
  25.         dv.Delete(0);  
  26.         dv[0].Row.AcceptChanges();  
  27.     }  
  28.     // Close the connection  
  29.     conn.Close();  
CAUTION: As you can see from Listing 9-16, the AcceptChanges() method removes a row permanently from the database. If you don't want to remove the row, call the RejectChanges() method.
 
The output of Listing 9-16 looks like figure 9-11 and 9-12.
 
Figure-9.11.jpg
 
Figure 9-11. The ListChange events output after adding a new row
 
Figure-9.12.jpg
 
Figure 9-12. The ListChanged events output after deleting a row
 

Conclusion

 
Hope this article would have helped you in understanding working with DataView and DataViewManager 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.


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.