다음을 통해 공유


Working with DataView Events 

You can use the ListChanged event of the DataView to determine if a view has been updated. Updates that raise the event include adding, deleting, or modifying a row in the underlying table; adding or deleting a column to the schema of the underlying table; and a change in a parent or child relationship. The ListChanged event also notifies you if the list of rows you are viewing has changed significantly due to the application of a new sort order or a filter.

The ListChanged event implements the ListChangedEventHandler delegate of the System.ComponentModel namespace and takes as input a ListChangedEventArgs object. You can determine what type of change has occurred using the ListChangedType enumeration value in the ListChangedType property of the ListChangedEventArgs object. For changes that involve adding, deleting, or moving rows, the new index of the added or moved row and the previous index of the deleted row can be accessed using the NewIndex property of the ListChangedEventArgs object. In the case of a moved row, the previous index of the moved row can be accessed using the OldIndex property of the ListChangedEventArgs object.

The DataViewManager also exposes a ListChanged event to notify you if a table has been added or removed, or if a change has been made to the Relations collection of the underlying DataSet.

The following code example shows how to add a ListChanged event handler.

AddHandler custView.ListChanged, _
  New System.ComponentModel.ListChangedEventHandler( _
  AddressOf OnListChanged)

Private Shared Sub OnListChanged( _
  sender As Object, args As System.ComponentModel.ListChangedEventArgs)
  Console.WriteLine("ListChanged:")
  Console.WriteLine(vbTab & "    Type = " & _
    System.Enum.GetName(args.ListChangedType.GetType(), _
    args.ListChangedType))
  Console.WriteLine(vbTab & "OldIndex = " & args.OldIndex)
  Console.WriteLine(vbTab & "NewIndex = " & args.NewIndex)
End Sub
custView.ListChanged  += new 
  System.ComponentModel.ListChangedEventHandler(OnListChanged);

protected static void OnListChanged(object sender, 
  System.ComponentModel.ListChangedEventArgs args)
{
  Console.WriteLine("ListChanged:");
  Console.WriteLine("\t    Type = " + args.ListChangedType);
  Console.WriteLine("\tOldIndex = " + args.OldIndex);
  Console.WriteLine("\tNewIndex = " + args.NewIndex);
}

See Also

Reference

DataView Class
ListChangedEventHandler Delegate

Other Resources

Creating and Using DataViews