Blue Theme Orange Theme Green Theme Red Theme
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
Search :       Advanced Search �

Author Rank :
Page Views :
Downloads : 0
Rating :
 Rate it
Level :
Become a Sponsor

This article has been excerpted from 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 sender is the source of the event and e is ListChangedEventArgs, which contains the event data. Table 9-8 defines the ListChangedEventArgs members.

Table 9-8. The ListChangedEventArgs members




Returns the way that list changed


Returns the new index of the item in the list


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

        protected static void OnListChanged(object sender, System.ComponentModel.ListChangedEventArgs args)
            MessageBox.Show("ListChanged: Type = " + args.ListChangedType
            + ", OldIndex = " + args.OldIndex
            + ", 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

        private void Form1_load(object sender, System.EventArgs e)
            OleDbConnection conn = new OleDbConnection();
            string strDSN = "provider=Microsoft.Jet.OLEDB.4.0;" +
            "Data Source= C:/Northwind.mdb";
             conn.ConnectionString = strDSN;
            string sql =
            "SELECT EmployeeID, LastName, FirstName FROM Employees";

             // Opening Connection

             // Create a data Adapter
            OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);

             // Create and fill DataSet
            DataSet ds = new DataSet();
            da.Fill(ds, "Employees");
            DataView dv = ds.Tables["Employees"].DefaultView;

             // Add DataView Event Handlers
            dv.ListChanged += new System.ComponentModel.ListChangedEventHandler

             // Add a row to the DataView
            dv.AllowEdit = true;
            DataRowView rw = dv.AddNew();
            rw["FirstName"] = "FName";
            rw["LastName"] = "LName";

             // Remove a row from the DataView
            if (dv.Count > 0)

             // Close the connection

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. The ListChange events output after adding a new row


Figure 9-12. The ListChanged events output after deleting a row


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.

This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET.

 [Top] Rate this article
 About the author
 Post a Feedback, Comment, or Question about this article

 � 2023  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.