ARTICLE

Working with DataView and DataViewManager Events in ADO.NET

Posted by Dinesh Beniwal Articles | ADO.NET in VB.NET May 31, 2010
In this article you will learn how to Work with DataView and DataViewManager Events in ADO.NET
 
Reader Level:

HTML clipboard

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 Sub ListChangedEventHandler(ByVal senderAs Object,ByVal e As ListChangedEventArges)
 
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
 

MEMBER

DESCRIPTION

ListChangedType

Returns the way that list 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

    Protected Shared Sub OnListChanged(ByVal sender As Object, ByVal args As System.ComponentModel.ListChangedEventArgs)

        MessageBox.Show((("ListChanged: Type = " & args.ListChangedType & ", OldIndex = ") + args.OldIndex & ", NewIndex = ") + args.NewIndex)

    End Sub

To test this application, you can create aWindowsapplication 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 Sub Form1_load(ByVal sender As Object,ByVal e As System.EventArgs)
        Dim conn As New OleDbConnection()
        Dim strDSN As String ="provider=Microsoft.Jet.OLEDB.4.0;" &"Data Source= C:/Northwind.mdb"
        conn.ConnectionString = strDSN
        Dim sql As String = "SELECT EmployeeID, LastName, FirstName FROM Employees"

        ' Opening Connection
        conn.Open()

        ' Create a data Adapter
        Dim daAs New OleDbDataAdapter(sql, conn)

        ' Create and fill DataSet
        Dim dsAs New DataSet()
        da.Fill(ds, "Employees")
        Dim dv As DataView = ds.Tables("Employees").DefaultView

        ' Add DataView Event Handlers

        AddHandler dv.ListChanged, AddressOf OnListChanged

        ' Add a row to the DataView
        dv.AllowEdit = True
        Dim rwAs DataRowView = dv.AddNew()
        rw.BeginEdit()
        rw("FirstName") ="FName"
        rw("LastName") ="LName"
        rw.EndEdit()

        ' Remove a row from the DataView
        If dv.Count > 0Then
            dv.Delete(0)
            dv(0).Row.AcceptChanges()
        End If

        ' Close the connection
        conn.Close()
   End Sub
 

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor