![]() |
Prerequisite Knowledge Object Model Overview: DataGridCollectionView Class DataGrid Fundamentals: Providing Data |
The items that are displayed in a view can be limited to one ore more filtering criteria using either the native CollectionView filtering and/or automatic filtering meaning that even if an item exists in the underlying data source, it does not necessarily mean that it is displayed in the view.
Native CollectionView Filtering
The data items that are displayed in a grid can be filtered using the Filter property of the DataGridCollectionViewBase or the Filter event of the DataGridCollectionViewSourceBase to which it is bound (see Example 1).
To refilter the data items displayed in a view the Refresh method can be called on the ItemsSource.
| VB.NET | Copy Code |
|---|---|
CType( Me.OrdersGrid.ItemsSource, DataGridCollectionView ).Refresh() | |
| C# | Copy Code |
|---|---|
( ( DataGridCollectionView )this.OrdersGrid.ItemsSource ).Refresh(); | |
Automatic Filtering 
In addition to the native CollectionView filtering, the DataGridCollectionView and DataGridDetailDescription classes also support automatic filtering, which provides Excel-like end-user filtering according to the distinct values of each column. Automatic filtering can be enabled by setting the AutoFilterMode property to And or Or (by default, None), indicating whether data items will be filtered according to all or at least one of the filtering criteria defined by each column's auto-filter control (see Example 2). The DistinctValuesConstraint property can also be set to determine if the distinct values are to be filtered according to the result of previous auto-filtering operations.
If a column does not need to support automatic filtering, it is recommended to set its corresponding DataGridItemProperty's CalculateDistinctValues to false or set the DataGridCollectionViewSource or DataGridDetailDescription's DefaultCalculateDistinctValues properties to false and only set CalculateDistinctValues to true for the item properties that will support auto-filtering.
The order in which the columns are automatically filtered can be determined by consulting the AutoFilterIndex property.
Through a DataGridItemProperty's DistinctValuesSortComparer and DistinctValuesEqualityComparer properties, an IComparer and IEqualityComparer can be provided to provide custom sorting and equality comparisons of the distinct values contained in an auto-filter control, respectively.
![]() |
If a column displays a custom type, automatic filtering will not function properly for that column unless the type implements the IComparable interface or a custom sort comparer is provided. |
Custom Distinct Values 
By default, the values that are displayed in the auto-filter drop down represent the distinct values as they are extracted from the underlying data source; however, custom distinct values can be provided for one or more item properties rather than the originally-extracted distinct values by handling their QueryDistinctValue event and returning the custom value (see Example 3).
Examples
All examples in this topic assume that the grid is bound to the Orders table of the Northwind database, unless stated otherwise.
Example 1: Filtering data items (Filter Event)
Example 2: Enabling automatic filtering
Example 3: Providing custom distinct values
DataGrid Fundamentals
Providing Data



Hide All