This is a migrated thread and some comments may be shown as answers.

Custom Grid Header Filter

5 Answers 145 Views
GridView
This is a migrated thread and some comments may be shown as answers.
band
Top achievements
Rank 1
band asked on 04 Oct 2016, 08:52 PM

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.  

5 Answers, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 07 Oct 2016, 09:38 AM
Hello,

I believe that our FilteringCollectionProperties 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.
0
band
Top achievements
Rank 1
answered on 15 Nov 2016, 08:05 PM

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)

 



0
Accepted
Yoan
Telerik team
answered on 18 Nov 2016, 02:48 PM
Hello,

Please note that there is no out-of-the-box way 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 . 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 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.
0
band
Top achievements
Rank 1
answered on 18 Jan 2017, 06:59 PM
Hi Yoan, I'm still having trouble recreating with the given tips. Could you point me in a direction where it would be MVVM? 
0
Yoan
Telerik team
answered on 23 Jan 2017, 03:05 PM
Hello,

I am sending you the modified version of the mentioned  example. All changes that I described in my previous reply are applied in the attached project. 

I hope it helps.

Regards,
Yoan
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
band
Top achievements
Rank 1
Answers by
Yoan
Telerik team
band
Top achievements
Rank 1
Share this question
or