Unplanned
Last Updated: 03 Mar 2022 08:49 by Alessandro
Alessandro
Created on: 03 Mar 2022 08:49
Category: GridView
Type: Feature Request
1
RadGridView: GridViewDateTimeView should support DateOnly type

With releasing .NET 6, there are TimeOnly and DateOnly types which would be more appropriate for managing such values:

https://devblogs.microsoft.com/dotnet/date-time-and-time-zone-enhancements-in-net-6/

It would be good to add support for these types in GridViewDateTimeView.

Currently, the following code gives an exception when entering edit mode: 

        public RadForm1()
        {
            InitializeComponent();
            DataTable dt = new DataTable();
            dt.Columns.Add("DateOnly", typeof(DateOnly));
            dt.Rows.Add(new DateOnly(2022,3,3));

            this.radGridView1.AutoGenerateColumns = false;
            GridViewDateTimeColumn dateColumn = new GridViewDateTimeColumn();
            dateColumn.FieldName = "DateOnly";
            this.radGridView1.Columns.Add(dateColumn);
            this.radGridView1.DataSource = dt;
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        }

 

 

Workaround: you can use the following custom TypeConverter

        public RadForm1()
        {
            InitializeComponent();
            DataTable dt = new DataTable();
            dt.Columns.Add("DateOnly", typeof(DateOnly));
            dt.Rows.Add(new DateOnly(2022,3,3));

            this.radGridView1.AutoGenerateColumns = false;
            GridViewDateTimeColumn dateColumn = new GridViewDateTimeColumn();
            dateColumn.DataType = typeof(DateTime);
            dateColumn.FieldName = "DateOnly";
            dateColumn.Format = DateTimePickerFormat.Custom;
            dateColumn.CustomFormat = "dd/MM/yyyy";
            dateColumn.FormatString = "{0:dd/MM/yyyy}";
            dateColumn.DataTypeConverter = new DateOnlyConverter();
            this.radGridView1.Columns.Add(dateColumn);
            this.radGridView1.DataSource = dt;
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        }


        public class DateOnlyConverter : TypeConverter
        {
            public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
            { 
                return destinationType == typeof(DateTime);
            }
            public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            { 
                if (value is DateOnly && destinationType == typeof(DateTime))
                {
                    DateOnly date = (DateOnly)value;
                    return new DateTime(date.Year, date.Month, date.Day);
                }
                
                return base.ConvertTo(context, culture, value, destinationType);
            }
            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 DateTime)
                {
                    DateTime date = (DateTime)value;
                    return new DateOnly(date.Year, date.Month, date.Day);
                }
                
                return base.ConvertFrom(context, culture, value);
            }
        }

 

    
0 comments