DateTimeColumn Format

6 posts, 0 answers
  1. Doug
    Doug avatar
    50 posts
    Member since:
    Jul 2012

    Posted 08 Oct 2012 Link to this post

    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.)
  2. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 11 Oct 2012 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Doug
    Doug avatar
    50 posts
    Member since:
    Jul 2012

    Posted 16 Oct 2012 Link to this post

    I'm getting closer to what I want.  I still have the braces and leading zero in the cell in edit mode.
  5. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 19 Oct 2012 Link to this post

    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.
  6. Doug
    Doug avatar
    50 posts
    Member since:
    Jul 2012

    Posted 19 Oct 2012 Link to this post

    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
  7. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 23 Oct 2012 Link to this post

    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.
Back to Top
UI for WinForms is Visual Studio 2017 Ready