The IBindingListView Interface Supporting Advanced Sorting and Filtering
The IBindingListView Functionality Data Binding" href="/data-binding/adding-ibindinglistview-functionality.html">IBindingListView interface supplements the data-binding capabilities of the IBindingList interface by adding support for multi-property sorts and filtering of the list.
As described in the previous section, IBindingList gives you simple sorting and searching capability. However, sometimes you need additional functionality. You may want to be able to sort on multiple properties or columns in a data collection at the same time, or you may want to filter the items presented by the collection based on some criteria without having to search for matches one at a time. The IBindingListView interface is designed exactly to address these needs.IBindingListView derives from IBindingList, so all the stuff described for IBindingList and all of its base interfaces applies here as well. The properties and methods ofBindingListView are described in Tables 7.13 and 7.14.
|
Name |
Type |
Description |
|
Filter |
String |
Gets or sets the string used as filter criteria for changing the data objects exposed by the collection. |
|
SortDescriptions |
ListSortDescriptionCollection |
Lets you inspect the current collection of sort descriptions that are applied to the collection. This is a read-only property. |
|
SupportsAdvancedSorting |
Boolean |
True if the collection allows you to perform multi-property sorts, false otherwise. This is a read-only property. |
|
SupportsFiltering |
Boolean |
True if the collection allows you to filter it by setting the Filter property, false otherwise. This is a read-only property. |
|
Method |
ApplySort(ListSortDescriptionCollection sortDescriptions) |
Lets you apply complex sort criteria that are based on one or more properties on the collection's data objects. You can remove the sort with the RemoveSort method inherited from the base IBindingList interface. |
|
void RemoveFilter() |
Lets you remove the current filter criteria and restore the list to the full collection of data. You can accomplish the same thing by setting the Filter property to a null string. |
To support advanced sorting, a collection must be able to set the order based on multiple properties on the data objects simultaneously. Before attempting to apply advanced sorting, the client code should check the SupportsAdvancedSorting property to see if it is true. If so, then the sort criteria are specified by constructing a collection dfistSortDescription objects and passing those to the
ApplySort method on the IBindingListView interface. The ListSortDescription type is pretty straightforward: it is just a container for a pair containing a PropertyDescriptor property and aSortDirection property. Typically, the order of these items in the ListSortDescriptionCollection determines the order in which the sort criteria are applied. To remove an advanced sort that has been applied, just call the RemoveSort method that was inherited from the IBindingList base class. If you want to access the sort descriptions that are currently in use for the collection, you can check the IsSorted property from the base interface, and then use theSortDescriptions property to obtain the collection of ListSortDescriptions currently in play.
If a collection supports filtering, it should return true from the SupportsFiltering property. You can then set a filter string using thiilter property, which should immediately modify what data objects are exposed by the collection if you were to iterate over it. The format of the filter string is going to be an implementation detail that will be specified by each collection type. For example, the DataView class supports SQL-like filter strings that match the strings permissible on theDataColumn. Expression property. If you are implementing your own collection type, you have to decide on syntax for filter strings that makes sense for your scenario, and then document it well so your collection's users know what filter strings they can provide.
If a filter has been applied to a collection, you can remove it with the RemoveFilter method. If you want to check what the current filter criteria is, just read the Filter property. This property should benull if there is no filter applied. In fact, this is an alternate way of removing a filter: just set the Filter property to null.
See Chapter 9for a sample implementation of thelBindingListView interface on the BindingListView<T> generic type developed there.
Post a comment