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

Cant add time in GridDateTimeColumn?

3 Answers 88 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jonathan Zee
Top achievements
Rank 1
Jonathan Zee asked on 07 Apr 2011, 09:52 AM
Hi guys,

this may sound stupid but how do you add edit the time in a GridDateTimeColumn in the grid view? Everytime i click on the column, the text box shows only the date component e.g.[ Thursday, 1 April 2011 ] which is fine but I would also like to be able to edit the time in the column... clicking on the dropdownbutton in the editor calls up the calendar but that only allows me to change the dates.

Please Advise.

Thanks







3 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Apr 2011, 11:46 AM
Hello Jonathan,

Please take a look at the following example:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
    private List<Test> list;
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
        list = new List<Test>();
        for (int i = 0; i < 10; i++)
        {
            list.Add(new Test
            {
                Id = i,
                Name = "Name " + i,
                Date = DateTime.Now.AddDays(1).AddHours(1).AddMinutes(1)
            });
        }
 
        radGridView1.DataSource = list;
    }
 
    void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
    {
        var dateColumn = radGridView1.Columns["Date"] as GridViewDateTimeColumn;
        dateColumn.Format = DateTimePickerFormat.Custom;
        var format = CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern + " " + CultureInfo.CurrentUICulture.DateTimeFormat.ShortTimePattern;
        dateColumn.CustomFormat = format;
        dateColumn.FormatInfo = CultureInfo.CurrentUICulture;
        dateColumn.FormatString = "{0:"+format+"}";
    }
}
 
public class Test
{
    public int Id
    {
        get;
        set;
    }
 
    public DateTime Date
    {
        get;
        set;
    }
 
    public String Name
    {
        get;
        set;
    }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP

0
Jonathan Zee
Top achievements
Rank 1
answered on 08 Apr 2011, 02:44 AM
Hi Emanuel,

Thanks for the help! It works great! On a side note, I wonder why the column is defaulted to setting of dates only and not the full DateTime format.

Regards

Jonathan.
0
Martin Vasilev
Telerik team
answered on 12 Apr 2011, 12:23 PM
Hi Jonathan Zee,

We have set DateTime column to use only the date part by default because this is most common scenario. Nevertheless I think Emanuel has provided a good and complete example how to implement this according to your requirements. Do not hesitate to contact us again if you have any other questions.

Kind regards,
Martin Vasilev
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Jonathan Zee
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Jonathan Zee
Top achievements
Rank 1
Martin Vasilev
Telerik team
Share this question
or