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

DateTimeColumn Format

5 Answers 237 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Doug
Top achievements
Rank 1
Doug asked on 08 Oct 2012, 11:06 PM
How do you set the properties on a DateTimeColumn when you want the date only or the time only?  I have the editor type working but for displaying only MM/dd/yyyy or hh:mm I can't figure out the combination.

Actually, change that.  I found the FormatString property and some examples of "{0:t}" and "{0:d}".  I have "{0:HH:mm:tt} and "{0:d}" working for me.  But when  I edit cells in the columns that are time values I see the curly braces and that leading 0 in the text.  How can I get those out?

(And if you drag that time column to the group area the formatting in the group row includes the date.)

5 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 11 Oct 2012, 12:53 PM
Hi Gary,

Thank you for writing.

The FormatString property sets the format of the grid cell when they are not in edit mode. To set the format for the cells when in edit mode, you need to set the Format property of the column to Custom and the CustomFormat with the desired format. Here is an example:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            GridViewDateTimeColumn dateColumn = new GridViewDateTimeColumn("Date");
            dateColumn.FormatString = "{0:d}";
            dateColumn.Format = DateTimePickerFormat.Custom;
            dateColumn.CustomFormat = "d";
            radGridView1.Columns.Add(dateColumn);
            dateColumn.Width = 100;
 
            GridViewDateTimeColumn timeColumn = new GridViewDateTimeColumn("Time");
            timeColumn.FormatString = "{0:HH:mm:tt}";
            timeColumn.Format = DateTimePickerFormat.Custom;
            timeColumn.CustomFormat = "HH:mm:tt";
            radGridView1.Columns.Add(timeColumn);
            timeColumn.Width = 100;
 
            radGridView1.Rows.Add(DateTime.Now, DateTime.Now);
         }
    }

More information regarding this matter is available here: http://www.telerik.com/help/winforms/gridview-columns-gridviewdatetimecolumn.html.

I hope this helps.

All the best,
Stefan
the Telerik team
You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
0
Doug
Top achievements
Rank 1
answered on 16 Oct 2012, 09:46 PM
I'm getting closer to what I want.  I still have the braces and leading zero in the cell in edit mode.
0
Stefan
Telerik team
answered on 19 Oct 2012, 01:37 PM
Hi Gary,

I was able to reproduce this. Can you please try to set the format of the editor itself:
public Form1()
{
    InitializeComponent();
 
    DataTable table = new DataTable();
    table.Columns.Add("Time", typeof(DateTime));
    radGridView1.DataSource = table;
 
    this.radGridView1.Columns["Time"].FormatString = "{0:HH:mm tt}";
 
    radGridView1.EditorRequired += new EditorRequiredEventHandler(radGridView1_EditorRequired);
    radGridView1.CellEditorInitialized += new GridViewCellEventHandler(radGridView1_CellEditorInitialized);
    radGridView1.Rows.Add(DateTime.Now, DateTime.Now);
}
 
void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    if (radGridView1.CurrentColumn.Name == "Time")
    {
        GridTimePickerEditor editor = e.ActiveEditor as GridTimePickerEditor;
        RadTimePickerElement element = editor.EditorElement as RadTimePickerElement;
        element.Format = "HH:mm tt";
    }
}
 
void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
{
    if (radGridView1.CurrentColumn.Name == "Time")
    {
        GridTimePickerEditor editor = new GridTimePickerEditor();
        RadTimePickerElement element = editor.EditorElement as RadTimePickerElement;
        e.EditorType = typeof(GridTimePickerEditor);
    }
}

Please let me know whether this works for you.
 
All the best,
Stefan
the Telerik team
You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
0
Doug
Top achievements
Rank 1
answered on 19 Oct 2012, 03:26 PM
Stefan,

That works.  Thank you.

I have issues with VS2005 and Visual RPG 8.x.  The build fails but it won't show me an error message.  The problem is in the CellEditorInitialized event handler with the line -  element.Format = "HH:mm tt";  (the VIsual RPG version of that).  I commented that out and used debug to set the format and the time displayed correctly in the editor.  I'll pass that issue along to ASNA.

I'm also working toward developing with VS2010 and including C#.  So I'll get this worked out in my project one way or another and your solution will be part of that.

Gary
0
Stefan
Telerik team
answered on 23 Oct 2012, 09:50 AM
Hello Gary,

I am glad I could help and I hope that you will solve the observed issue painlessly.

Let us know if you need anything else.
 
Kind regards,
Stefan
the Telerik team
You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
Tags
GridView
Asked by
Doug
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Doug
Top achievements
Rank 1
Share this question
or