Date formatting

6 posts, 0 answers
  1. SSirica
    SSirica avatar
    50 posts
    Member since:
    Jan 2007

    Posted 22 Sep 2014 Link to this post

    I have a date coming from the database in the format yyyymmdd.  I would like it to display in the drop down as mm/dd/yyyy.  I thought I could accomplish this with the FormatString, but as I'm finding out...not so much.  Anybody got any ideas how to accomplish this?

    Thanks
    Steve
  2. Dimitar
    Admin
    Dimitar avatar
    1413 posts

    Posted 25 Sep 2014 Link to this post

    Hi Steve,

    Thank you for writing.

    In order to set the format string you should cast the auto generated column to GridViewDateTimeColumn. In addition you can set the format for the cell text and the editor:
    void Form1_Load(object sender, EventArgs e)
    {
        
        GridViewDateTimeColumn dateColumn = radGridView1.Columns["Date"]as GridViewDateTimeColumn;
        dateColumn.Format = DateTimePickerFormat.Custom;
        dateColumn.CustomFormat = "MM/dd/yyyy";
        dateColumn.FormatString = "{0:MM/dd/yyyy}";
     
    }

    I hope this helps.

    Regards,
    Dimitar
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. SSirica
    SSirica avatar
    50 posts
    Member since:
    Jan 2007

    Posted 25 Sep 2014 in reply to Dimitar Link to this post

    Thanks for your response, but that didn't work.  still showing up as yyyymmdd.  is there any was to call a function to do the formatting?
  5. Dimitar
    Admin
    Dimitar avatar
    1413 posts

    Posted 26 Sep 2014 Link to this post

    Hi Steve,

    Thank you for writing back.

    I have prepared and attached a small sample to show you how to format the column. Could you please check it and let me know how it fits in your case?

    I am looking forward to your reply.
     
    Regards,
    Dimitar
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  6. SSirica
    SSirica avatar
    50 posts
    Member since:
    Jan 2007

    Posted 26 Sep 2014 Link to this post

    Thanks for your reply and example.  Formatting a normal date is not the issue.  The part of the puzzle you're missing is that the date is in the format of yyyymmdd, so today would be returned from the database as 20140926.  
  7. Dimitar
    Admin
    Dimitar avatar
    1413 posts

    Posted 29 Sep 2014 Link to this post

    Hi Steve,

    Thank you for writing back.

    If I understand correctly you are getting the date as a string from the database and you want to format it. This can be achieved by using the CellFormatting event and manually formatting the text of the cells:
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.Column.Name == "StringDate"&& e.CellElement.Value!= null)
        {
            DateTime temp = DateTime.ParseExact(e.CellElement.Value.ToString(), "yyyymmdd", CultureInfo.InvariantCulture);
           e.CellElement.Text = string.Format("{0:MM/dd/yyyy}", temp);
        }
    }

    I hope this helps.

    Regards,
    Dimitar
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
UI for WinForms is Visual Studio 2017 Ready