DataView and DataViewManager Events working in VB.NET

In this article you will learn how to Work with DataView and DataViewManager Events in ADO.NET
  • 3615
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.

 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.