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

Override GridTimeDateTimeColumn autofilling to current time

1 Answer 72 Views
GridView
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 04 Aug 2015, 11:25 AM

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.

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 04 Aug 2015, 02:13 PM
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 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. 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
Tags
GridView
Asked by
David
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or