![]() |
Prerequisite Knowledge Object Model Overview: DataGridCollectionView Class Basic Concepts: 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 DataGridCollectionView or the Filter event of the DataGridCollectionViewSource 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.
![]() |
If a column displays a custom type, automatic filtering will not function properly for that column unless the type implements the IComparable interface. |
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)
The following example demonstrates how to filter the data items displayed in a grid using the Filter event. Only the data items whose ShipVia property value is "3" will be displayed in the grid.
| XAML | Copy Code |
|---|---|
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"> | |
The following code provides the implementation of the ShipViaFilter event. This code should be placed in the "code-behind" of your XAML page.
| VB.NET | Copy Code |
|---|---|
| |
| C# | Copy Code |
|---|---|
| |
Example 2: Enabling automatic filtering
The following example demonstrates how to enable automatic filtering, disabling it for the columns that will not support it and filtering the distinct values of the ShipCity column.
| XAML | Copy Code |
|---|---|
| |
Basic Concepts
Providing Data
Advanced Concepts
Custom Auto-filtering Controls



Hide All