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

Using DateTimeOffset in column filter

2 Answers 503 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aleksey 311
Top achievements
Rank 1
Aleksey 311 asked on 07 Jun 2014, 02:03 AM
Hi!

When I try to filter a column of type DateTimeOffset, I get an error like: "System.InvalidCastException: Invalid cast "System.DateTime" to quot;System.DateTimeOffset"."

Please tell me how to solve this problem. Thank you!

2 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 11 Jun 2014, 07:05 AM
Hello,

This is not currently supported. The DataSourceRequest will always create a DateTime and not a DateTimeOffset. You should use a DateTime property in order to avoid the exception. If this is not possible in your scenario then I can suggest to convert the filter value to DateTimeOffset in the request filters e.g.
public static class Extensions
{
    public static void MapFilters(this IList<IFilterDescriptor> filters, Action<FilterDescriptor> action)
    {
        foreach (var filter in filters)
        {
            if (filter is CompositeFilterDescriptor)
            {
                MapFilters(((CompositeFilterDescriptor)filter).FilterDescriptors, action);
            }
            else
            {
                action((FilterDescriptor)filter);
            }
        }
    }
}
if (request.Filters.Any())
{
    request.Filters.MapFilters(descriptor =>
        {
            if (descriptor.Member == "MyField")
            {                           
                descriptor.Value = new DateTimeOffset((DateTime)descriptor.Value);
            }
        });
}


Regards,
Daniel
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Aleksey 311
Top achievements
Rank 1
answered on 18 Jun 2014, 05:42 PM
Thank you! Its working for me.
Tags
Grid
Asked by
Aleksey 311
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Aleksey 311
Top achievements
Rank 1
Share this question
or