Problem with DateTimeColumn and Converter.

6 posts, 0 answers
  1. zzz
    zzz avatar
    6 posts
    Member since:
    Jul 2011

    Posted 13 Sep 2011 Link to this post

    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.
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 14 Sep 2011 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 16 Sep 2011 Link to this post

    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 >>

  5. zzz
    zzz avatar
    6 posts
    Member since:
    Jul 2011

    Posted 19 Sep 2011 Link to this post


    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

  6. zzz
    zzz avatar
    6 posts
    Member since:
    Jul 2011

    Posted 21 Sep 2011 Link to this post

    The DateTime filtering is still not working (i think this was not clearly evident in my last posting). 
    I would appreciate your help.

    Thanks.
  7. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 22 Sep 2011 Link to this post

    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 >>

Back to Top
UI for WinForms is Visual Studio 2017 Ready