MultiColumnComboBox with virtualization - Filter on numbers

1 Answer 21 Views
MultiColumnComboBox
Stefan
Top achievements
Rank 1
Iron
Iron
Veteran
Stefan asked on 07 Jan 2022, 09:25 AM

We're using a virtualized MultiColumnComboBox including the filter functionality 'contains'.

Unfortunately a filter on an integer field throws an error 'FormatException' on ToDataSourceResult

Is there a default way of making this work, or do we have to modify the DataSourceRequest manually?

If the latter, is there an example of how to do it?

1 Answer, 1 is accepted

Sort by
1
Ivan Danchev
Telerik team
answered on 12 Jan 2022, 10:14 AM

Hi Stefan,

Neither "Contains" nor "Startswith"  are valid filter operators for type int. You can modify the filter descriptor in the DataSourceRequest as shown below and use a valid operator for the int type (IsLessThan, IsGreaterThan, IsEqualTo, IsNotEqualTo, etc.). In the example below the ItemNumber is the fields that is filtered. It is of type int and ModifyFilters method applies the IsEqualTo filter operator. ModifyFilters is called in the GetItems action which is the "Read" end point for the MultiColumnComboBox:

private void ModifyFilters(IEnumerable<IFilterDescriptor> filters)
{
    if (filters.Any())
    {
        foreach (var filter in filters)
        {
            var descriptor = filter as FilterDescriptor;
            if (descriptor != null && descriptor.Member == "ItemNumber")
            {
                descriptor.MemberType = typeof(Int32);
                descriptor.Value = Int32.Parse(descriptor.Value.ToString());
                descriptor.Operator = Kendo.Mvc.FilterOperator.IsEqualTo;
            }
            else if (filter is CompositeFilterDescriptor)
            {
                ModifyFilters(((CompositeFilterDescriptor)filter).FilterDescriptors);
            }
        }
    }
}

public ActionResult GetItems([DataSourceRequest] DataSourceRequest request)
{
    if (request.Filters != null)
    {
        ModifyFilters(request.Filters);
    }

    return Json(GetOrders().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}

private static IEnumerable<SampleModel> GetOrders()
{
    var data = Enumerable.Range(0, 500).Select(i => new SampleModel
    {
        ItemNumber = i,
        Description = "Item" + i
    });

    return data;
}

Regards,
Ivan Danchev
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
MultiColumnComboBox
Asked by
Stefan
Top achievements
Rank 1
Iron
Iron
Veteran
Answers by
Ivan Danchev
Telerik team
Share this question
or