Custom Grid Header Filter

4 posts, 0 answers
  1. band
    band avatar
    12 posts
    Member since:
    Jun 2016

    Posted 04 Oct Link to this post

    Hi,

    At the moment, one of the columns has no filter. In my model, that column is a collection of strings. For the grid, I append them together separating by a comma. 

    Person.cs

    Name
    Collection<strings> Tags

    John Doe, new List("Apples", "Oranges")

    In my grid, it appears as "John Doe" in the first column and "Apples, Oranges" in the second column.

    In my database, I do have a collection of all the tags, I was wondering if I can create a custom filter such that it'll check if it contains the tags I select to filter by.  

  2. Yoan
    Admin
    Yoan avatar
    1066 posts

    Posted 07 Oct Link to this post

    Hello,

    I believe that our FilteringCollectionProperties sdk example will help you achieving your goal.

    Regards,
    Yoan
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
  4. band
    band avatar
    12 posts
    Member since:
    Jun 2016

    Posted 15 Nov Link to this post

    Hi Yoan,

    This worked but I'm wondering how can I enhance it? 

    Using that sdk example, how would I be able to filter by checkboxes.

    The filter drop down would have a checkbox for each weekday available in the list (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, not sunday because it doesn't appear)

     



  5. Yoan
    Admin
    Yoan avatar
    1066 posts

    Posted 18 Nov Link to this post

    Hello,

    Please note that there is no out-of-the-box way for achieving this and further customizations are needed. Here are some tips:

    You will have to show the distinct values list. To do so, you need to remove the ShowDistinctFilters property from the constructor of the custom column. Then in the OnDistinctValuesLoading event, you need to set an ItemsSource for the DistinctValues listbox. For example:
    private void OnDistinctValuesLoading(object sender, GridViewDistinctValuesLoadingEventArgs e)
            {
                if (e.Column is CollectionPropertyColumn)
                {
                    e.ItemsSource =  new List<string>() { "Monday", "Tuesday", "Wednesday"};
                }
            }

    Then you need to rework the overridden CreateFilterExpression method of the CollectionPropertyColumnFilterDescriptor. In this lines:
    IFieldFilterDescriptor fieldFilter = ((IColumnFilterDescriptor)this).FieldFilter;
     
            Expression f1Expression = null;
            if (fieldFilter.Filter1.Value != FilterDescriptor.UnsetValue)
            {
                // Build the UPPER field filter, i.e. field filter 1
                // "Monday"
                ConstantExpression f1Value = Expression.Constant(fieldFilter.Filter1.Value);
     
                // person.WorkingDays.Cast<object>().Contains("Monday")
                f1Expression = Expression.Call(
                    CollectionPropertyColumnFilterDescriptor.GenericContainsMethod
                    , genericCollectionPropertyAccessor
                    , f1Value);

    the FiledFilter property is used for building a filter expression. You can modify this code in a similar way to use the distinct value. For example:
    var distinctValue = ((IColumnFilterDescriptor)this).DistinctFilter.DistinctValues.First();
     
    Expression f1Expression = null;
    if (distinctValue != null)
    {
        // Build the UPPER field filter, i.e. field filter 1
        // "Monday"
        ConstantExpression f1Value = Expression.Constant(distinctValue);
     
        // person.WorkingDays.Cast<object>().Contains("Monday")
        f1Expression = Expression.Call(
            CollectionPropertyColumnFilterDescriptor.GenericContainsMethod
            , genericCollectionPropertyAccessor
            , f1Value);


    Regards,
    Yoan
    Telerik by Progress
    Telerik UI for WPF is ready for Visual Studio 2017 RC! Learn more.
Back to Top