Xceed DataGrid for Silverlight Documentation
Filtering Data

The data that is contained in a grid can be filtered, programmatically or through end-user interaction, to display a subset of data. Programmatically, data can be filtered through the Filter event, which allows more complex filtering such as comparisons between two fields, by providing a filter expression (see FilterExpression class) via the FilterExpression property, or through combinations of both. 

Through interactions with a FilterRow, an end user can modify how the data in a grid is filtered. Each value that is entered into a cell (see FilterCell class) becomes part of a logical-and filter expression (see Filter Expressions section below), which is imbedded into the data query that is used to request data and filters the items before they are returned. A filter cell's IsCaseSensitive property determines if the value entered into the cell is to be treated in a case-sensitive manner or not.

Through the filter row's FilterExpression property, the filter expression can be retrieved or one can be provided (e.g., initial filter criteria).


Filter row in Excel 2007 black theme

By default, a filter row is not displayed; however, one can be added to the header or footer sections of a grid (see Headers and Footers topic).

<sldg:DataGridControl.FixedHeaders>
   <sldg:FilterRow/>
</sldg:DataGridControl.FixedHeaders>
 
<!--or-->
 
<sldg:DataGridControl DefaultHeadersFooters="ColumnManagerRow, GroupByControl, FilterRow"/>
The filter entered in a filter cell will not be applied until an operator is selected. If one is already selected and the filter is changed, then the new filter will be applied automatically.

The text displayed by a filter operator and its visibility is determined by its corresponding *Visible *(e.g., ContainedInFilterOperatorVisible) and *Text (e.g., ContainedInFilterOperatorText) properties, which are defined by FilterCell class. These properties can be set via an implicit style, or by providing an explicit style to a column's FilterCellStyle property.

Regardless of the value of the *FilterOperatorVisible properties, if the data type of the parent column (see DataType property) does not allow the operator to be displayed, it will remain hidden.

Filter Expressions

Filter expressions, which can be provided through a grid's or filter row's FilterExpression properties, are imbedded into the data query that is used to request data and filters the items before they are returned. The filter expressions that are provided can be a simple, single-value expressions (see FilterExpression class), or more complex expressions that use one or more logical filter expressions (see AndFilterExpression, NotFilterExpression, and OrFilterExpression classes). Each filter expression defines a value, the name of the member by whose values the items are to be filtered, and the filter operator to use (see FilterOperator enumeration). If string or date and time values are to be filtered, the case or time sensitivity of the value can also be specified.

If a logical filter expression is used, at least two operand filter expressions must be provided. Once the filter expression has been created, it is considered "frozen" and its values can no longer be modified.

Values entered into the filter row are neither case sensitive nor time sensitive. If a case- or time- sensitive value is provided via a filter expression, an exception will be thrown. In other words, when creating a filter expression that is to be assigned to a filter row programmatically, its IsCaseSenstive and IsTimeSensitive properties must both be set to false.

If a string value is specified, the IsCaseSenstive property must be set to true or false; otherwise, an exception will be thrown.

<sldg:DataGridControl x:Name="sldgDataGridControl"
                      ItemsSource="{Binding Path=Orders}">
    <sldg:DataGridControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <sldg:SignatureThemeResourceDictionary />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </sldg:DataGridControl.Resources>
    
    <sldg:DataGridControl.FixedHeaders>
        <CheckBox Content="Display only French-Speaking Countries"
                  Checked="CheckBox_Checked"
                  Unchecked="CheckBox_Unchecked"
                  Style="{StaticResource SignatureThemeCheckBoxStyle}"/>
        <sldg:FilterRow>
            <sldg:FilterRow.FilterExpression>
                <sldg:AndFilterExpression>
                    <sldg:FilterExpression MemberName="ShipVia"
                                           Value="3"
                                           FilterOperator="Equal" />
                    <sldg:FilterExpression MemberName="OrderDate"
                                           Value="2008/01/01"
                                           FilterOperator="GreaterThanOrEqual"
                                           IsTimeSensitive="False"/>                            
                </sldg:AndFilterExpression>
            </sldg:FilterRow.FilterExpression>
        </sldg:FilterRow>
    </sldg:DataGridControl.FixedHeaders>
</sldg:DataGridControl>

Private m_frenchCountries As New List( Of String)( New String( 27 )
       { "Belgium", "Benin", "Burkina-Faso", "Burundi", 
         "Cameroon", "Canada", "Central African Republic", "Chad", "Comoros", 
         "Congo", "Djibouti", "France", "Gabon", "Guinea", "Haiti", "Ivory Coast", 
         "Luxembourg", "Madagascar", "Mali", "Monaco", "Niger", "Rwanda", 
         "Senegal", "Seychelles", "Switzerland", "Togo", "Vanuatu" } )
 
 
 
Private Function CreateFilterExpression() As OrFilterExpression 
   Dim childExpressions As New List( Of FilterExpressionBase )()
 
   Dim country As String
   For Each country In m_frenchCountries 
      Dim expression As New FilterExpression( "ShipCountry", FilterOperator.Equal, country, False )
      childExpressions.Add( expression )
   Next country
 
 
 
   Return New OrFilterExpression( childExpressions )
End Function
 
 
 
Private m_filterExpression As OrFilterExpression = Nothing
Private Sub CheckBox_Checked( ByVal sender As Object, ByVal e As RoutedEventArgs )
   If m_filterExpression Is Nothing Then
      m_filterExpression = Me.CreateFilterExpression()
   End If
 
 
 
   Me.sldgDataGridControl.FilterExpression = m_filterExpression
End Sub
 
 
 
Private Sub CheckBox_Unchecked( ByVal sender As Object, ByVal e A RoutedEventArgs )
   Me.sldgDataGridControl.FilterExpression = Nothing
End Sub

private List<string> m_frenchCountries = new List<string>( new string[ 27 ] 
       { "Belgium", "Benin", "Burkina-Faso", "Burundi", 
         "Cameroon", "Canada", "Central African Republic", "Chad", "Comoros", 
         "Congo", "Djibouti", "France", "Gabon", "Guinea", "Haiti", "Ivory Coast", 
         "Luxembourg", "Madagascar", "Mali", "Monaco", "Niger", "Rwanda", 
         "Senegal", "Seychelles", "Switzerland", "Togo", "Vanuatu" } );
 
 
 
private OrFilterExpression CreateFilterExpression()
{
   List<FilterExpressionBase> childExpressions = new List<FilterExpressionBase>();
 
   foreach( string country in m_frenchCountries )
   {
      FilterExpression expression = new FilterExpression( "ShipCountry", FilterOperator.Equal, country, false );
      childExpressions.Add( expression );
   }
 
 
 
   return new OrFilterExpression( childExpressions );
}
 
 
 
private OrFilterExpression m_filterExpression = null;
private void CheckBox_Checked( object sender, RoutedEventArgs e )
{
   if( m_filterExpression == null )
      m_filterExpression = this.CreateFilterExpression();
 
 
 
   this.sldgDataGridControl.FilterExpression = m_filterExpression;
}
 
 
 
private void CheckBox_Unchecked( object sender, RoutedEventArgs e )
{
   this.sldgDataGridControl.FilterExpression = null;
}

Filter Event

The Filter event is raised for each item in the data source that has not already been excluded by a filter expression to determine if it passes a filter. Unlike filter expressions, the Filter event can be used to provide more complex filtering such as comparisons between two fields.

The Filter event is executed locally, meaning that all items will be retrieved from the server in order to determine those that pass the filter. In a virtualized environment, it is not recommended to use the Filter event unless dealing with a very limited number of items. 
Private Sub DataGridControl_Filter( ByVal sender As Object, ByVal e As DataGridFilterEventArgs )
   Dim order As Order = CType( e.Item, Order )
 
 
 
   If order Is Nothing Then
      e.Accepted = False
   Else
      e.Accepted = ( order.ShippedDate > order.RequiredDate )
   End If
End Sub

private void DataGridControl_Filter( object sender, DataGridFilterEventArgs e )
{
   Order order = e.Item as Order;
 
 
 
   if( order == null )
   {
      e.Accepted = false;
   }
   else
   {
      e.Accepted = ( order.ShippedDate > order.RequiredDate );
   }
}

 

Send Feedback