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
I believe that our FilteringCollectionProperties example will help you achieving your goal.
Regards,
Yoan
Telerik by Progress
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)
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
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