Working with DataView and DataViewManager Events


The DataView and DataViewManager objects both define a ListChanged event. The ListChanged event occurs when a row is added to or deleted from a DataView or DataViewManager object. The ListChangedEventHandler method handles the ListChanged event and is defined as follows:

 Public Delegate Sub ListChangedEventHandler(_    ByVal sender As Object, _    ByVal e As ListChangedEventArgs _ ) 

In this example, sender is the source of the event and e is the ListChangedEventArgs containing the event data.

The ListChangedEventArgs has the members defined in Table 5-8.

Table 5-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 5-16 shows the OnListChanged_Handler.

Listing 5-16: DataView OnListChanged Event Handler

start example
 Public Sub OnListChanged_Handler(ByVal src As Object, _    ByVal args As System.ComponentModel.ListChangedEventArgs)    MessageBox.Show("ListChanged: Type = " + args.ListChangedType & _    ", OldIndex = " + args.OldIndex & _    ", NewIndex = " + args.NewIndex)  End Sub 
end example

To test this application, you can create a Windows application and add the code in Listing 5-17 to the form load event or a button click event handler. As you can see from Listing 5-17, the code creates a DataView object, adds a new row to the DataView, and then removes the first row from the DataView. The adding and removing of rows is responsible for firing the OnListChanged event handler.

Listing 5-17: The DataView Event Handler Caller

start example
 Dim ConnectionString As String = "Integrated Security=SSPI;" & _    "Initial Catalog=Northwind;Data Source=MCB;"    Dim conn As SqlConnection = New SqlConnection()    conn.ConnectionString = ConnectionString    ' Open the connection    conn.Open()    Dim sql As String = "SELECT EmployeeId, LastName, FirstName FROM Employees"    ' Create a data adapter    Dim da As SqlDataAdapter = New SqlDataAdapter(sql, conn)    ' Create and Fill DataSet    Dim ds As DataSet = New DataSet()    da.Fill(ds, "Employees")    Dim dv As DataView = ds.Tables("Employees").DefaultView    ' Add DataView Event Handlers    AddHandler dv.ListChanged, New System.ComponentModel.ListChangedEventHandler _    (AddressOf OnListChanged_Handler)    ' Add a row to the DataView    dv.AllowEdit = True    Dim rw As DataRowView = dv.AddNew()    rw.BeginEdit()    rw("FirstName") = "FName"    rw("LastName") = "LName"    rw.EndEdit()    ' Remove a row from the DataView    If dv.Count > 0 Then      dv.Delete(0)      dv(0).Row.AcceptChanges()    End If    ' Close the connection    conn.Close()    conn.Dispose()    ' Remove DataView Event Handlers    RemoveHandler dv.ListChanged, AddressOf OnListChanged_Handler 
end example

Caution

As you can see from Listing 5-17, the AcceptChanges() method removes a row permanently from the database. If you don't want to remove the row, call RejectChanges instead.




Applied ADO. NET(c) Building Data-Driven Solutions
Applied ADO.NET: Building Data-Driven Solutions
ISBN: 1590590732
EAN: 2147483647
Year: 2006
Pages: 214

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net