Custom Filtering using a Combo Box

Thread is closed for posting
1 posts, 0 answers
  1. 53DA46C0-EBE5-4E39-9E64-F210852A74C7
    53DA46C0-EBE5-4E39-9E64-F210852A74C7 avatar
    1 posts
    Member since:
    Oct 2008

    Posted 08 Oct 2008 Link to this post

    Requirements

    RadControls version

    Q2 2008 SP 1
    .NET version   3.5
    Visual Studio version 2008 SP 1
    programming language

    C#

     
    PROJECT DESCRIPTION
    In the sample project a RadGridView uses combo boxes to perform filtering. First, we create a control template for the DistinctFilterControl, the control that realizes the filtering process.
     
            <ControlTemplate x:Key="FilterControlTemplate" TargetType="{x:Type telerik:DistinctFilterControl}"
                <StackPanel> 
                    <StackPanel Orientation="Horizontal"
                        <ComboBox IsEditable="True" IsReadOnly="False" Name="comboBoxDistinctValues" 
                                    MaxHeight="500" HorizontalAlignment="Stretch" Width="70" 
                                    ItemsSource="{TemplateBinding DistinctValues}" 
                                    Loaded="ComboBox_Loaded"  
                                    SelectionChanged="ComboBox_SelectionChanged" /> 
                        <telerik:ClearFilterButton Margin="0,0,0,5" Content="Clear Filter" 
                                                Target="{Binding ElementName=comboBoxDistinctValues, Mode=OneTime}"  
                                                FilterDescription ="{TemplateBinding FilterDescription}" /> 
                    </StackPanel> 
                </StackPanel> 
            </ControlTemplate> 
            <Style TargetType="{x:Type telerik:DistinctFilterControl}"
                <Setter Property="Template" Value="{StaticResource FilterControlTemplate}" /> 
            </Style> 

    You can see the combo box (named "comboBoxDistinctValues") that contains the values you can filter by. Our button, that clears your filter, is retained in the template.

    You will notice that the events Loaded and SelectionChanged of the combo box are handled in the code-behind. The handler of Loaded:

     
    ComboBox comboBox = (ComboBox) sender; 
    DistinctFilterControl filterControl = this.GetVisualParent<DistinctFilterControl>(comboBox); 
    this.PopulateDistinctValues(string.Empty, filterControl); 

    calls the method "PopulateDistinctValues":
     
    GridViewHeaderCell headerCell = this.GetVisualParent<GridViewHeaderCell>(filterControl); 
     
    string propertyName = ((IColumnInfo) headerCell).PropertyName; 
     
    GridViewDataControl gridControl = this.GetVisualParent<GridViewDataControl>(filterControl); 
    filterControl.DistinctValues = gridControl.ItemsControl.RecordManager.GetDistinctValues(propertyName); 

    which fills the combo box with the values you can use for filtering. The event handler for SelectionChanged:

     
    ComboBox comboBox = (ComboBox) sender; 
    DistinctFilterControl filterControl = this.GetVisualParent<DistinctFilterControl>(comboBox); 
    FieldFilterDescription filterDescription = filterControl.FilterDescription; 
     
    IList valueCollection; 
    if (e.AddedItems.Count > 0) 
       valueCollection = e.AddedItems; 
       foreach (object value in valueCollection) 
       { 
           filterDescription.Values.Add(value); 
       } 
     
    if (e.RemovedItems.Count > 0) 
        valueCollection = e.RemovedItems; 
        foreach (object value in valueCollection) 
        { 
            if ((comboBox.Items.IndexOf(value) != -1)) 
                filterDescription.Values.Remove(value); 
        } 

    changes the filtering whenever you select a new value in the combo box.
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.