DekGenius.com
[ Team LiB ] Previous Section Next Section

28.4 Events Reference

ListChanged

ListChangedEventHandler ListChanged;

Fires when the information in a DataView changes, including when DataRowView objects are added, deleted, modified, or moved. This event provides a ListChangedEventArgs object with information about the type of change, and the old and new index of the item, if appropriate. This event is rarely used in application programming (in which case the DataTable events are much more useful) but is typically reserved for creating custom data bound controls.

Event Argument

ListChangedEventArgs e

Exposes three properties. ListChangedType provides an enumerated value that indicates how the list changed. NewIndex and OldIndex indicate where an item moved, if the event is in response to a single row change. For example, if an item is moved from position 0 to position 1, NewIndex is 1, and OldIndex is 0. If an item is deleted, NewIndex contains the former index of the deleted item, and OldIndex isn't used.

Table 28-2 lists the valid values for the ListChangedType enumeration.

Table 28-2. ListChangedType values

Value

Description

ItemAdded

An item was added to the list at position NewIndex.

ItemChanged

The item at NewIndex was changed.

ItemDeleted

The item at NewIndex was deleted.

ItemMoved

An item moved in the list from OldIndex to NewIndex.

PropertyDescriptorAdded, PropertyDescriptorChanged, and PropertyDescriptorDeleted

A PropertyDescriptor was added, changed, or deleted, and this changed the schema.

Reset

The list has changed substantially. Any listeners should reread the entire DataView.

Example

Here's an example that handles the ListChanged event and displays some information to a debug window:

private void OnListChanged(object sender, ListChangedEventArgs args)
{
    Debug.WriteLine("ListChanged:");
    Debug.WriteLine("\t    Type = " + args.ListChangedType);
    Debug.WriteLine("\tOldIndex = " + args.OldIndex);
    Debug.WriteLine("\tNewIndex = " + args.NewIndex);
}

Note

If you make a change that affects multiple rows, such as changing the Sort order, the DataView doesn't fire one ListChanged event for each row. Instead, it fires a ListChanged event with a ListChangedType of Reset.

    [ Team LiB ] Previous Section Next Section