Home > Net >  How to define my own filter while working with Telerik RadGridView control?
How to define my own filter while working with Telerik RadGridView control?

Time:12-28

I'm working on a C# application, where I'm trying to create my own filtering mechanism for filtering a Telerik RadGridView control.

My RadGridView control looks as follows (in the xaml file, I've left some open lines for showing the part where I define my filter):

<telerik:RadGridView x:Name="PartsGridView"
                     AutoGenerateColumns="False"
                     CanUserFreezeColumns="False"
                     FilteringMode="Popup"
                     FilterOperatorsLoading="Grid_FilterOperatorsLoading"
                     ItemsSource="{Binding PagedSource, ElementName=PartsRadDataPager}"
                     IsSynchronizedWithCurrentItem="False"
                     SelectedItem="{Binding SelectedPart}"
                     SelectionMode="Single"
                     ShowInsertRow="False"
                     EnableRowVirtualization="True"
                     RowLoaded="PartsGridView_RowLoaded"
                     FieldFilterEditorCreated="FieldFilterEditorCreated"
                     IsFilteringAllowed="True"
                     DistinctValuesLoading="PartsGridView_DistinctValuesLoading"
                     Grouped="PartsGridView_Grouped">
    <telerik:RadGridView.GroupDescriptors>
        <telerik:GroupDescriptor Member="Name" DisplayContent="Article"/>
    </telerik:RadGridView.GroupDescriptors>
    <telerik:RadGridView.FilterDescriptors/>
    <telerik:RadGridView.Columns>
        ...
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Article.CustomerReference}" 
                                    Header="Client"
                                    IsReadOnly="True"
                                    Name="Client">
        
            <telerik:GridViewDataColumn.FilteringControl>
                <local:Myfilter Column="{Binding ElementName=Client}"/>
            </telerik:GridViewDataColumn.FilteringControl>
            
        </telerik:GridViewDataColumn>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

The corresponding source code is as follows (I've only shown the OnFilter, it's the idea to extend the filtering possibilities to regular expressions, but let's start making the regular filter work first):

public partial class MyFilter : System.Windows.Controls.UserControl, 
                                IFilteringControl
{
    private GridViewDataColumn column;
    private CompositeFilterDescriptor compositeFilter;
    private Telerik.Windows.Data.FilterDescriptor textFilter;

    private void OnFilter(object sender, RoutedEventArgs e)
    {
        //var reg = new Regex(textBox.Text.Replace("*", ".*"));

        // var descriptor = new FilterDescriptor<string> 
        //                  { FilteringExpression = o => reg.IsMatch(o) };

        compositeFilter = new CompositeFilterDescriptor();

        textFilter = new Telerik.Windows.Data.FilterDescriptor(Column.Name
            , Telerik.Windows.Data.FilterOperator.IsEqualTo
            , null);

        compositeFilter.FilterDescriptors.Add(textFilter);

        textFilter.Value = TextBox;

        if (!Column.DataControl.FilterDescriptors.Contains(compositeFilter))
        {
            Column.DataControl.FilterDescriptors.Add(compositeFilter);
        }

        IsActive = true;
    }

When debugging, I go into the OnFilter() function but instead of just filtering, I don't see any data in the RadGridView anymore.

Does anybody have an idea what I'm doing wrong?

Edit: In case the question is not clear, don't hesitate asking for more information.

CodePudding user response:

Try this:

private void OnFilter(object sender, RoutedEventArgs e)
{
    //var reg = new Regex(textBox.Text.Replace("*", ".*"));

    // var descriptor = new FilterDescriptor<string> 
    //                  { FilteringExpression = o => reg.IsMatch(o) };

    //compositeFilter = new CompositeFilterDescriptor();

    textFilter = new Telerik.Windows.Data.FilterDescriptor(Column.Name
        , Telerik.Windows.Data.FilterOperator.IsEqualTo
        , null);

    //compositeFilter.FilterDescriptors.Add(textFilter);

    textFilter.Value = TextBox;

    if (!Column.DataControl.FilterDescriptors.Contains(textFilter))
    {
        Column.DataControl.FilterDescriptors.Add(textFilter);
    }

    IsActive = true;
}

CodePudding user response:

u can also try this:

private void OnFilter(object sender, RoutedEventArgs e)
{
    compositeFilter = new CompositeFilterDescriptor();
    textFilter = new Telerik.Windows.Data.FilterDescriptor(Column.Name, Telerik.Windows.Data.FilterOperator.IsEqualTo, null);
    compositeFilter.FilterDescriptors.Add(textFilter);
    textFilter.Value = TextBox;
    if (!Column.DataControl.FilterDescriptors.Contains(compositeFilter))
    {
        Column.DataControl.FilterDescriptors.Add(compositeFilter);
    }
    IsActive = true;
}
  • Related