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

Problem with DateTimeColumn and Converter.

5 Answers 261 Views
GridView
This is a migrated thread and some comments may be shown as answers.
zzz
Top achievements
Rank 1
zzz asked on 13 Sep 2011, 08:58 AM
Hello!
I use a GridView bound to a data table. One of the columns in the data table stores a date information in Unix format (milliseconds since 1970). I have used a converter similar to the one described in the converting data types example.
When i use "DateTime" for the CanConvertTo method and "long" for the "CanConvertFrom" the column shows the milliseconds value and not a valid DateTime string. Changing the  "CanConvertTo" to "long" and the   "CanConvertFrom" method to "DateTime"  still keeping the "ConvertTo" method converting from "long" to "DateTime" i get valid date strings in the GridView. When I try to filter on that column I get an "InvalidCastException" in the "ConvertTo" method where the "Value" is a "DateTime" object and the destination type is Int64. 
When i change the "ConvertTo" and and "ConvertFrom" methods to check for "value" type and transform to "DateTime" iv "Value" type is "long" or "long" if "value" type is "DateTime".
Using this I get a "FilterExpressionException" with the inner exception message {"Cannot perform '>=' operation on System.Int64 and System.DateTime."} 
When i then take into account the "destinationType" in the convert method and make a transformation of the "value" object to the selected destination type the date string is again not valid (as in the first case).

Have I missed something or is there a bug?
Thanks for your help.

5 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 14 Sep 2011, 07:41 AM
Hello,

Could you please post a small sample here? It will go a lot faster if i have something to do some tests and try to help you figure this out.

Best Regards,
Emanuel Varga
0
Martin Vasilev
Telerik team
answered on 16 Sep 2011, 11:15 AM
Hello Gerhard,

Please, could you confirm that you have found a solution to this issue. If not, please answer the question from Emanuel. Sending us a sample application which reproduces the issue will allow us to investigate your particular case and provide you with accurate assistance. You can open a new support ticket in order to attach your project.

I am looking forward to your reply.

All the best,
Martin Vasilev
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
zzz
Top achievements
Rank 1
answered on 19 Sep 2011, 01:53 PM

I used the following row definition:
GridViewDateTimeColumn dateColumn = new GridViewDateTimeColumn();
dateColumn.DataTypeConverter = new DateTimeConverter(); 
dateColumn.FieldName = "Date";
dateColumn.Name = "Date";
dateColumn.HeaderText = "Date";
dateColumn.Format = DateTimePickerFormat.Custom;
dateColumn.FormatString = "{0:dd.MM.yyyy}";
gridView.Columns.Add(dateColumn);
The "Date" column in the source data is of type long.
Converter:

public class DateTimeConverter : TypeConverter 
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
    
        return destinationType == typeof(long) ; 
    
    
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
    
        if (value is long) 
        
            return DateTime(1970, 1, 1, 0, 0, 0, 0).AddMilliseconds((long)value); 
        
        else 
        
            return (long)Math.Floor((DateTime)value - DateTime(1970, 1, 1, 0, 0, 0, 0)); 
        
    
    
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
    
        return sourceType == typeof(DateTime) ; 
    
    
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 
    
        if (value is long) 
        
             return DateTime(1970, 1, 1, 0, 0, 0, 0).AddMilliseconds((long)value); 
        
        else 
        
             return (long)Math.Floor((DateTime)value - DateTime(1970, 1, 1, 0, 0, 0, 0)); 
        
    
}

Hoe that helps.
Thanks

0
zzz
Top achievements
Rank 1
answered on 21 Sep 2011, 08:33 AM
The DateTime filtering is still not working (i think this was not clearly evident in my last posting). 
I would appreciate your help.

Thanks.
0
Stefan
Telerik team
answered on 22 Sep 2011, 05:05 PM
Hello Gerhard,

Thank you for the provided code.

In your scenario, when you try to filter the column, the filtering mechanism receives a number instead of a valid DateTime and it cannot handle comparing number against DateTime. A number is returned, instead of a DateTime, since the custom TypeConverter is used only for displaying the data in the grid, and not for storing it. However, this scenario seems quite reasonable and this is why I will add it to our PITS system as a feature request. Feel free to follow this link, where you can add your vote for this feature.

Your Telerik points have been updated for this request. 

Meanwhile, before filling the grid with data, you can convert your numeric values into DateTime, and the fill the column with the converted values. This way you will not need to work with a custom TypeConverter, and all your operations will perform correctly.

I hope that you find this information useful. If there is anything else we can assist you with, do not hesitate to contact us.
 
All the best,
Stefan
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
zzz
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Martin Vasilev
Telerik team
zzz
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or