DateTime column formatting

6 posts, 0 answers
  1. Phi
    Phi avatar
    19 posts
    Member since:
    Jun 2007

    Posted 01 Jul 2007 Link to this post

    Hi,

    I have a DateTime column on a RadGridView. How do I set formatting for that column?
    I have tried adding GridViewDataColumn and GridViewDateTimeColumn to the MasterGridViewTemplate, then seting the DataTextFormatString property of the column to "{0:dd/MMM/yyyy}";
    but that doesn't work.

    Currently I am doing a work-around, where I subscribe to the OnGridViewCellFormatting event, detect if it's a date value and re-format it there, but this solution isn't really ideal.

    Please advise!

    Regards,
    Phi Le

  2. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 03 Jul 2007 Link to this post

    Hi Phi,

    Thank you for question.
     
    You can set the data bound column formatting with the following code:

    GridViewDataColumn newColumn = new GridViewDataColumn("Date");//databound column
    this.radGridView1.MasterGridViewTemplate.Columns.Add( newColumn );
    newColumn.DataTextFormatString = "{0:dd/MMM/yyyy}";

    If the column is not a data bound column, you should subscribe for GridViewCellFormatting event and reformat the value dynamically like you did it.
     

    Greetings,
    Peter
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Phi
    Phi avatar
    19 posts
    Member since:
    Jun 2007

    Posted 03 Jul 2007 Link to this post

    Hello! Thanks for your response.

    My columns are databound, so actually I did follow your suggestion originally. Here is the code I used:

    DataTable dt = new DataTable();  
    dt.Columns.Add("col1"typeof (int));  
    dt.Columns.Add("col2"typeof (DateTime));  
     
    dt.Rows.Add(new object[] {1, DateTime.Now});  
    dt.Rows.Add(new object[] {2, DateTime.Now.AddDays(1.0)});  
     
    this.radGridView1.MasterGridViewTemplate.AutoGenerateColumns = false;  
     
    GridViewDataColumn col1 = new GridViewDataColumn("col1");  
    this.radGridView1.MasterGridViewTemplate.Columns.Add(col1);  
     
    GridViewDataColumn col2 = new GridViewDataColumn("col2");  
    this.radGridView1.MasterGridViewTemplate.Columns.Add(col2);  
    col2.DataTextFormatString = "{0:dd/MMM/yyyy}";  
     
    this.radGridView1.DataSource = dt; 

    but the date column still doesn't format correctly. Is there something I have missed?

    Cheers
    Phi
  5. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 04 Jul 2007 Link to this post

    Hello Phi,

    We could not reproduce this issue - I have attached the files I have used for my tests. It may be best to send us your application, so that we can review it and provide a proper solution.

    At this time, I have only one suggestion - if you want to use special editor for datetime, please replace

     new GridViewDataColumn( "col2" )

    with

     new GridViewDateTimeColumn( "col2" )


    Regards,
    Peter
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  6. Phi
    Phi avatar
    19 posts
    Member since:
    Jun 2007

    Posted 04 Jul 2007 Link to this post

    Hi Peter!

    I see the problem now, I was using a standard GridViewDataColumn instead of the GridViewDateTimeColumn.

    It appears the datetime formatting doesn't work if you do not use the GridViewDateTimeColumn. Maybe this could be something you could look into.

    Thanks very much for clearing this up.

    Cheers
    Phi
  7. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 05 Jul 2007 Link to this post

    We are glad to be of help, Phi! If we could be of further assistance, please do not hesitate to write us back.

     
    Greetings,
    Peter
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
Back to Top
UI for WinForms is Visual Studio 2017 Ready