Override GridTimeDateTimeColumn autofilling to current time

2 posts, 0 answers
  1. David
    David avatar
    1 posts
    Member since:
    Mar 2015

    Posted 04 Aug 2015 Link to this post

    Hi,

    I have a time ​column on a RadGridView. If the field is null, it defaults to the current time of day when the user starts typing in a new value. I want to change this behavior.

    I need it to remain blank, and then default to 00:00 when the user start typing. FYI, the property on the datasource is a nullable DateTime.

    Thanks.

  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 04 Aug 2015 Link to this post

    Hello David,

    Thank you for writing.
     
    Following the provided information I have prepared a sample project with GridViewDateTimeColumn containing null values. When the editor is activated for a cell with null value, the editor is blank. Please refer to the attached gif file illustrating the behavior on my end. However, if you start typing the MaskDateTimeProvider will create a valid date considering the current DateTime. In order to keep the time part empty until you enter the desired time, you can use free date format. Here is a sample code snippet:
    public Form1()
    {
        InitializeComponent();
     
        List<Item> items = new List<Item>();
        for (int i = 0; i < 20; i++)
        {
            if (i % 2 == 0)
            {
                items.Add(new Item(i, "Item" + i, DateTime.Now.AddDays(i).AddHours(i)));
            }
            else
            {
                items.Add(new Item(i, "Item" + i, null));
            }
        }
     
        this.radGridView1.DataSource = items;
        this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
     
        GridViewDateTimeColumn dateTimeColumn = this.radGridView1.Columns["Date"] as GridViewDateTimeColumn;
        dateTimeColumn.FormatString = "{0:dd/MM/yyyy HH:mm}";
        //specify the editor's format
        dateTimeColumn.Format = DateTimePickerFormat.Custom ;
        dateTimeColumn.CustomFormat = "dd/MM/yyyy HH:mm";
     
        this.radGridView1.CellEditorInitialized += radGridView1_CellEditorInitialized;
    }
     
    private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        RadDateTimeEditor dtPicker = e.ActiveEditor as RadDateTimeEditor;
        if (dtPicker != null && dtPicker.Value == null)
        {
            RadDateTimePickerElement el = dtPicker.EditorElement as RadDateTimePickerElement;
            el.TextBoxElement.MaskType = MaskType.FreeFormDateTime;
            el.NullDate = new DateTime(1800,1,1,0,0,0);
            el.SetToNullValue();
        }
    }
     
    public class Item
    {
        public int Id { get; set; }
     
        public string Name { get; set; }
     
        public Nullable<DateTime> Date { get; set; }
     
        public Item(int id, string name, Nullable<DateTime> date)
        {
            this.Id = id;
            this.Name = name;
            this.Date = date;
        }
    }

    In order to introduce any custom behavior of the RadDateTimePickerElement behavior, you can replace the DateTimePickerElement.TextBoxElement.Provider with a derivative of the MaskDateTimeProvider where you can override the KeyPress method for example and introduce the desired behavior.

    I hope this information helps. Should you have further questions I would be glad to help.
     
    Regards,
    Dess
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top