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

Column format for Date

1 Answer 89 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kim
Top achievements
Rank 1
Kim asked on 04 Sep 2013, 09:25 PM
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

1 Answer, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 06 Sep 2013, 12:57 PM
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 >>
Tags
GridView
Asked by
Kim
Top achievements
Rank 1
Answers by
Nikolay
Telerik team
Share this question
or