Filtering Data

Glossary Item Box

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 This feature is available only in the Professional Edition

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.

ShowExample 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">
  <Grid.Resources>
    <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                                       
Source="{Binding Source={x:Static Application.Current},
                                                        
Path=Orders}"
                                       
Filter="ShipViaFilter"/>

  </Grid.Resources>

  <xcdg:DataGridControl x:Name="OrdersGrid"
                        ItemsSource="{Binding Source={StaticResource cvs_orders}}"/>
</Grid>

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
Private Sub ShipViaFilter( sender As Object, e As FilterEventArgs )

  Dim value As Object = CType( e.Item, System.Data.DataRow )( "ShipVia" )

  If( Not value Is Nothing ) And ( Not value Is DBNull.Value ) Then
    If CInt( value ) = 3 Then

      e.Accepted = True

    Else

      e.Accepted = False

    End If
  End If

End Sub
C# Copy Code
private void ShipViaFilter( object sender, FilterEventArgs e )
{
  object value = ( ( System.Data.DataRow )e.Item )[ "ShipVia" ]; 

  if( ( value != null ) && ( value != DBNull.Value ) )
  {
    if( ( int )value == 3 )
    {

     e.Accepted = true;
    }
    else
    {
     e.Accepted = false;
    }
  }
    
}

ShowExample 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

<Grid>
  <Grid.Resources>

  <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                                     
Source="{Binding Source={x:Static Application.Current},
                                                      
Path=Orders}"
                                     
AutoFilterMode="And"
                                     
DistinctValuesConstraint="Filtered"
                                     
AutoCreateItemProperties="False">      

       <xcdg:DataGridCollectionViewSource.ItemProperties>
         <xcdg:DataGridItemProperty Name="ShipCountry"
                                          Title="Country"/>

         <xcdg:DataGridItemProperty Name="ShipCity"
                                   
Title="City"/>

        <xcdg:DataGridItemProperty Name="ShipAddress"
                                   Title="Address"/>
        <xcdg:DataGridItemProperty Name="ShipPostalCode"
                                   Title="Postal Code"/>

         <xcdg:DataGridItemProperty Name="ShipName"
                                   
Title="Name"
                                   
CalculateDistinctValues="False"/>
         
<xcdg:DataGridItemProperty Name="OrderDate"
                                   
Title="Order Date"
                                   
CalculateDistinctValues="False"/>               
         
<xcdg:DataGridItemProperty Name="Freight"
                                   
CalculateDistinctValues="False"/>

      </xcdg:DataGridCollectionViewSource.ItemProperties>
    </xcdg:DataGridCollectionViewSource>       
  </Grid.Resources>

  <xcdg:DataGridControl x:Name="OrdersGrid"
                        ItemsSource="{Binding Source={StaticResource cvs_orders}}"/>
</Grid>

 

See Also

Basic Concepts
Providing Data

Advanced Concepts
Custom Auto-filtering Controls