The following page provides a list of examples that demonstrate how to filter data items. For more filtering-related information, refer to the Filtering Data topic.
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 (value IsNot Nothing) AndAlso (value <> 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; } } } |
The next example demonstrates how to filter data items using the Filter predicate delegate.
VB.NET |
Copy Code |
---|---|
Dim view As New DataGridCollectionView( Orders ) view.Filter = New Predicate(Of Object)( ShipViaFilter ) dataGridControl.ItemsSource = view Private Function ShipViaFilter( item As Object ) As Boolean Dim value As Object = TryCast( item, System.Data.DataRow )( "ShipVia" ) If (value IsNot Nothing) AndAlso (value <> DBNull.Value) Then If CInt( value ) = 3 Then Return True End If End If Return false End Function |
C# |
Copy Code |
---|---|
DataGridCollectionView view = new DataGridCollectionView( Orders ); view.Filter = new Predicate<object>( ShipViaFilter ); dataGridControl.ItemsSource = view; private bool ShipViaFilter( object item ) { object value = ( ( System.Data.DataRow )item )[ "ShipVia" ]; if( ( value != null ) && ( value != DBNull.Value ) ) { if( ( int )value == 3 ) return true; } return false; } |
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> |
VB.NET |
Copy Code |
---|---|
Dim view As New DataGridCollectionView( Orders, GetType( System.Data.DataRow ), False, False ) view.AutoFilterMode = AutoFilterMode.And view.DistinctValuesConstraint = DistinctValuesConstraint.Filtered view.ItemProperties.Add( New DataGridItemProperty( "ShipCountry", GetType( String ) ) ) view.ItemProperties.Add( New DataGridItemProperty( "ShipCity", GetType( String ) ) ) view.ItemProperties.Add( New DataGridItemProperty( "ShipAddress", GetType( String ) ) ) view.ItemProperties.Add( New DataGridItemProperty( "ShipPostalCode", GetType( String ) ) ) Dim shipName As New DataGridItemProperty( "ShipName", GetType( String ) ) shipName.CalculateDistinctValues = False view.ItemProperties.Add( shipName ) Dim orderDate As New DataGridItemProperty( "OrderDate", GetType( DateTime ) ) orderDate.CalculateDistinctValues = False view.ItemProperties.Add( orderDate ) Dim freight As New DataGridItemProperty( "Freight", GetType( Double ) ) freight.CalculateDistinctValues = False view.ItemProperties.Add( freight ) dataGridControl.ItemsSource = view |
C# |
Copy Code |
---|---|
DataGridCollectionView view = new DataGridCollectionView( Orders, typeof( System.Data.DataRow ), false, false ); view.AutoFilterMode = AutoFilterMode.And; view.DistinctValuesConstraint = DistinctValuesConstraint.Filtered; view.ItemProperties.Add( new DataGridItemProperty( "ShipCountry", typeof( string ) ) ); view.ItemProperties.Add( new DataGridItemProperty( "ShipCity", typeof( string ) ) ); view.ItemProperties.Add( new DataGridItemProperty( "ShipAddress", typeof( string ) ) ); view.ItemProperties.Add( new DataGridItemProperty( "ShipPostalCode", typeof( string ) ) ); DataGridItemProperty shipName = new DataGridItemProperty( "ShipName", typeof( string ) ); shipName.CalculateDistinctValues = false; view.ItemProperties.Add( shipName ); DataGridItemProperty orderDate = new DataGridItemProperty( "OrderDate", typeof( DateTime ) ); orderDate.CalculateDistinctValues = false; view.ItemProperties.Add( orderDate ); DataGridItemProperty freight = new DataGridItemProperty( "Freight", typeof( double ) ); freight.CalculateDistinctValues = false; view.ItemProperties.Add( freight ); dataGridControl.ItemsSource = view; |
Providing a new auto-filter control style
The following example demonstrates how to provide the ShipCountry column with a new style for its associated AutoFilterControl that will only allow single selection.
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> <Style x:Key="autoFilterControlStyle" TargetType="{x:Type xcdg:AutoFilterControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <ListBox x:Name="PART_DistinctValuesHost" SelectionMode="Single"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </Grid.Resources> <xcdg:DataGridControl x:Name="OrdersGrid" ItemsSource="{Binding Source={StaticResource cvs_orders}}"> <xcdg:DataGridControl.Columns> <xcdg:Column FieldName="ShipCountry" AutoFilterControlStyle="{StaticResource autoFilterControlStyle}"/> </xcdg:DataGridControl.Columns> </xcdg:DataGridControl> </Grid> |
Creating external auto-filter controls
The following example demonstrates how to use a ComboBox as an auto-filter control to automatically filter the content of the ShipCountry column. ComboBox controls do not support multiple selections; therefore, the values of the target column will only be filtered by 1 value.
Since, by default, the auto-filter control in the column-manager-cell drop downs support multiple selections, it is recommended to deactivate the drop down by setting the AllowAutoFilter property of the ColumnManagerRow to false to hide the column-manager cells' auto-filter controls and prevent unexpected synchronization behavior between the controls that have the same auto-filter target column or different selection modes.
XAML |
Copy Code |
---|---|
<Grid> <Grid.Resources> <xcdg:DataGridCollectionViewSource x:Key="cvsOrders" Source="{Binding Source={x:Static Application.Current},Path=Orders}" AutoFilterMode="And" AutoCreateItemProperties="False" DefaultCalculateDistinctValues="False" DistinctValuesConstraint="Filtered" DistinctValuesRefreshNeeded="cvsOrders_DistinctValuesRefreshNeeded"> <xcdg:DataGridCollectionViewSource.ItemProperties> <xcdg:DataGridItemProperty Name="ShipCountry" Title="Country" CalculateDistinctValues="True" /> <xcdg:DataGridItemProperty Name="ShipCity" Title="City" CalculateDistinctValues="True" /> <xcdg:DataGridItemProperty Name="ShipAddress" Title="Address" /> <xcdg:DataGridItemProperty Name="ShipPostalCode" Title="Postal Code" /> <xcdg:DataGridItemProperty Name="ShipName" Title="Name" /> <xcdg:DataGridItemProperty Name="OrderDate" Title="Order Date" /> <xcdg:DataGridItemProperty Name="Freight" /> </xcdg:DataGridCollectionViewSource.ItemProperties> </xcdg:DataGridCollectionViewSource> </Grid.Resources> <DockPanel> <StackPanel Margin="20,0,0,0" Orientation="Horizontal" DockPanel.Dock="Top" HorizontalAlignment="Left"> <xcdg:AutoFilterControl x:Name="ShipCountryAutoFilterControl" AutoFilterColumn="{Binding ElementName=dataGrid, Path=Columns[ShipCountry]}" AutoFilterContext="{Binding ElementName=dataGrid, Path=DataGridContext}"> <xcdg:AutoFilterControl.Template> <ControlTemplate TargetType="{x:Type xcdg:AutoFilterControl}"> <StackPanel> <Button Content="Clear Country filter" Command="xcdg:AutoFilterControl.ClearAutoFilterValues" CommandTarget="{Binding ElementName=ShipCountryAutoFilterControl}" /> <ComboBox x:Name="PART_DistinctValuesHost" DropDownOpened="ShipCountryAutoFilterControl_DropDownOpened" /> </StackPanel> </ControlTemplate> </xcdg:AutoFilterControl.Template> </xcdg:AutoFilterControl> <xcdg:AutoFilterControl x:Name="ShipCityAutoFilterControl" AutoFilterColumn="{Binding ElementName=dataGrid, Path=Columns[ShipCity]}" AutoFilterContext="{Binding ElementName=dataGrid, Path=DataGridContext}"> <xcdg:AutoFilterControl.Template> <ControlTemplate TargetType="{x:Type xcdg:AutoFilterControl}"> <StackPanel> <Button Content="Clear City filter" Command="xcdg:AutoFilterControl.ClearAutoFilterValues" CommandTarget="{Binding ElementName=ShipCityAutoFilterControl}" /> <ComboBox x:Name="PART_DistinctValuesHost" DropDownOpened="ShipCityAutoFilterControl_DropDownOpened" /> </StackPanel> </ControlTemplate> </xcdg:AutoFilterControl.Template> </xcdg:AutoFilterControl> </StackPanel> <xcdg:DataGridControl x:Name="dataGrid" ItemsSource="{Binding Source={StaticResource cvsOrders}}"> <xcdg:DataGridControl.View> <xcdg:TableflowView UseDefaultHeadersFooters="False"> <xcdg:TableflowView.FixedHeaders> <DataTemplate> <xcdg:GroupByControl /> </DataTemplate> <DataTemplate> <xcdg:ColumnManagerRow AllowAutoFilter="False" /> </DataTemplate> </xcdg:TableflowView.FixedHeaders> </xcdg:TableflowView> </xcdg:DataGridControl.View> </xcdg:DataGridControl> </DockPanel> </Grid> |
The following code provides the code-behind implementation of the DistinctValuesRefreshNeeded and DropDownOpened events.
Providing custom distinct values
The following example demonstrates how to provide custom distinct values that will display the only the month in columns that display DateTime values and that will filter according to a value range for a decimal column.
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}" AutoFilterMode="And" DefaultCalculateDistinctValues="False"> <xcdg:DataGridCollectionViewSource.ItemProperties> <xcdg:DataGridItemProperty Name="OrderDate" QueryDistinctValue="DataGridItemProperty_QueryDistinctValue_Date" CalculateDistinctValues="True"/> <xcdg:DataGridItemProperty Name="RequiredDate" QueryDistinctValue="DataGridItemProperty_QueryDistinctValue_Date" CalculateDistinctValues="True" /> <xcdg:DataGridItemProperty Name="ShippedDate" QueryDistinctValue="DataGridItemProperty_QueryDistinctValue_Date" CalculateDistinctValues="True" /> <xcdg:DataGridItemProperty Name="Freight" QueryDistinctValue="DataGridItemProperty_QueryDistinctValue_Range" CalculateDistinctValues="True" /> </xcdg:DataGridCollectionViewSource.ItemProperties> </xcdg:DataGridCollectionViewSource> </Grid.Resources> <xcdg:DataGridControl x:Name="OrdersGrid" ItemsSource="{Binding Source={StaticResource cvs_orders}}"/> </Grid> |
VB.NET |
Copy Code |
---|---|
Private Sub DataGridItemProperty_QueryDistinctValue_Date( ByVal sender As Object, ByVal e As QueryDistinctValueEventArgs ) If TypeOf e.DataSourceValue Is DateTime Then e.DistinctValue = CDate( e.DataSourceValue ).ToString( "MMMM" ) End If End Sub Private Sub DataGridItemProperty_QueryDistinctValue_Range( ByVal sender As Object, ByVal e As QueryDistinctValueEventArgs ) If TypeOf e.DataSourceValue Is Decimal Then Dim value As Decimal = CDec( e.DataSourceValue ) If value <= 100 Then e.DistinctValue = "0 - 100" ElseIf( value > 100 And value <= 500 ) Then e.DistinctValue = "101 - 500" Else e.DistinctValue = "500+" End If End If End Sub |
C# |
Copy Code |
---|---|
private void DataGridItemProperty_QueryDistinctValue_Date( object sender, QueryDistinctValueEventArgs e ) { if( e.DataSourceValue is DateTime ) { e.DistinctValue = ( ( DateTime )e.DataSourceValue ).ToString( "MMMM" ); } } private void DataGridItemProperty_QueryDistinctValue_Range( object sender, QueryDistinctValueEventArgs e ) { if( e.DataSourceValue is decimal ) { decimal value = ( decimal )e.DataSourceValue; if( value <= 100 ) { e.DistinctValue = "0 - 100"; } else if( value > 100 && value <= 500 ) { e.DistinctValue = "101 - 500"; } else { e.DistinctValue = "500+"; } } } |
Using a filter row
The following example demonstrates how to add a FilterRow to the fixed headers of a grid's view that will allow the data items in the grid to be filtered according to the user-specified filter criteria.
XAML |
Copy Code |
---|---|
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"> <Grid.Resources> <xcdg:DataGridCollectionViewSource x:Key="cvs_products" Source="{Binding Source={x:Static Application.Current}, Path=Orders}" /> </Grid.Resources> <xcdg:DataGridControl x:Name="OrdersGrid" ItemsSource="{Binding Source={StaticResource cvs_products}}"> <xcdg:DataGridControl.View> <xcdg:TableView> <xcdg:TableView.FixedHeaders> <DataTemplate> <xcdg:FilterRow Background="Pink" /> </DataTemplate> </xcdg:TableView.FixedHeaders> </xcdg:TableView> </xcdg:DataGridControl.View> </xcdg:DataGridControl> </Grid> |
Providing default filter criteria
The following example demonstrates how to provide default filter criteria that will initially filter the data items in a grid through the FilterRow.
XAML |
Copy Code |
---|---|
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid" xmlns:s="clr-namespace:System;assembly=mscorlib"> <Grid.Resources> <xcdg:DataGridCollectionViewSource x:Key="cvs_orders" Source="{Binding Source={x:Static Application.Current}, Path=Orders}"> <xcdg:DataGridCollectionViewSource.ItemProperties> <xcdg:DataGridItemProperty Name="ShipCountry"> <xcdg:DataGridItemProperty.FilterCriterion> <xcdg:EqualToFilterCriterion Value="Canada" /> </xcdg:DataGridItemProperty.FilterCriterion> </xcdg:DataGridItemProperty> <xcdg:DataGridItemProperty Name="EmployeeID"> <xcdg:DataGridItemProperty.FilterCriterion> <xcdg:OrFilterCriterion> <xcdg:OrFilterCriterion.FirstFilterCriterion> <xcdg:EqualToFilterCriterion> <s:Int32>2</s:Int32> </xcdg:EqualToFilterCriterion> </xcdg:OrFilterCriterion.FirstFilterCriterion> <xcdg:OrFilterCriterion.SecondFilterCriterion> <xcdg:EqualToFilterCriterion> <s:Int32>3</s:Int32> </xcdg:EqualToFilterCriterion> </xcdg:OrFilterCriterion.SecondFilterCriterion> </xcdg:OrFilterCriterion> </xcdg:DataGridItemProperty.FilterCriterion> </xcdg:DataGridItemProperty> </xcdg:DataGridCollectionViewSource.ItemProperties> </xcdg:DataGridCollectionViewSource> </Grid.Resources> <xcdg:DataGridControl x:Name="OrdersGrid" ItemsSource="{Binding Source={StaticResource cvs_orders}}"> <xcdg:DataGridControl.View> <xcdg:TableView> <xcdg:TableView.FixedHeaders> <DataTemplate> <xcdg:FilterRow Background="Pink" /> </DataTemplate> </xcdg:TableView.FixedHeaders> </xcdg:TableView> </xcdg:DataGridControl.View> </xcdg:DataGridControl> </Grid> |
Enabling and disabling advanced filtering
The following example demonstrates how to set advanced filter mode to Always, and how to disable advanced filtering on at least one column.
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}" /> </Grid.Resources> <xcdg:DataGridControl x:Name="OrdersGrid" ItemsSource="{Binding Source={StaticResource cvs_orders}}"> <xcdg:DataGridControl.Columns> <xcdg:Column FieldName="ShipCountry" AllowAdvancedFilter="False" /> </xcdg:DataGridControl.Columns> <xcdg:DataGridControl.View> <xcdg:TableflowView AdvancedFilterMode="Always" /> </xcdg:DataGridControl.View> </xcdg:DataGridControl> </Grid> |
Localize the filter operators
The following example demonstrates how to set a style to localize the operators.
XAML |
Copy Code |
---|---|
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"> <Grid.Resources> <ResourceDictionary> <xcdg:DataGridCollectionViewSource x:Key="cvs_orders" Source="{Binding Source={x:Static Application.Current}, Path=Orders}" /> <Style x:Key="filterExpressionEditorStyle" TargetType="xcdg:FilterExpressionEditor"> <Setter Property="FilterOperatorsText"> <Setter.Value> <col:Hashtable> <s:String x:Key="{x:Type xcdg:GreaterThanFilterCriterion}">></s:String> <s:String x:Key="{x:Type xcdg:GreaterThanOrEqualToFilterCriterion}">>=</s:String> <s:String x:Key="{x:Type xcdg:LessThanFilterCriterion}"><</s:String> <s:String x:Key="{x:Type xcdg:LessThanOrEqualToFilterCriterion}"><=</s:String> <s:String x:Key="{x:Type xcdg:EqualToFilterCriterion}">=</s:String> <s:String x:Key="{x:Type xcdg:DifferentThanFilterCriterion}"><></s:String> </col:Hashtable> </Setter.Value> </Setter> </Style> </ResourceDictionary> </Grid.Resources> <xcdg:DataGridControl x:Name="OrdersGrid" ItemsSource="{Binding Source={StaticResource cvs_orders}}"> <xcdg:DataGridControl.Columns> <xcdg:Column FieldName="ShipCountry" FilterExpressionEditorStyle="{StaticResource filterExpressionEditorStyle}" /> </xcdg:DataGridControl.Columns> </xcdg:DataGridControl> </Grid> |