Column format for Date

2 posts, 0 answers
  1. Kim
    Kim avatar
    47 posts
    Member since:
    Jul 2013

    Posted 04 Sep 2013 Link to this post

    I am trying to format a GridViewTextBoxColumn that contains a date using format string {0:MM/dd/yyyy}

    However its ignoring the format and the 12:00 am is displaying behind the date.

    How do i get a GridViewTextBoxColumn to format dates? 

    Thanks.

    Kim
  2. Nikolay
    Admin
    Nikolay avatar
    1805 posts

    Posted 06 Sep 2013 Link to this post

    Hello Kim,

    Applying the desired FormatString would work if the value which the FormatString looks up is of type DateTime. However, when you place a DateTime value in a GridViewTextBoxColumn it is being converted to an ordinary string and FormatString has no effect on it. Therefore, if you need to format the value, it should be stored in a GridViewDataTimeColumn which will keep its DateTime type. Alternatively, if you for some reason need to use GridViewTextBoxColumn, you can manually format the shown text using the CellFormatting event:

    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.CellElement.Value != null)
        {
            int indexOfSpace = e.CellElement.Value.ToString().IndexOf(' ');
            e.CellElement.Text = e.CellElement.Value.ToString().Substring(0, indexOfSpace);
        }
    }

    I hope this helps. Regards,
    Nikolay
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top