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

DataFormatString Does Not Work For Dates

2 Answers 235 Views
Grid
This is a migrated thread and some comments may be shown as answers.
jfkrueger
Top achievements
Rank 1
jfkrueger asked on 11 Nov 2008, 06:37 PM
I'm simply trying to format a date in a bound column to mm/dd/yyyy format. Here is the column:

<telerik:GridBoundColumn DataField="DOB" EmptyDataText="&amp;nbsp;"   
     HeaderText="DOB" SortExpression="DOB" UniqueName="DOB"   
     DataFormatString="{0:MM/dd/yyyy}" DataType="System.DateTime" 
     HtmlEncode="false">  
  <HeaderStyle Wrap="False" /> 
</telerik:GridBoundColumn> 

Looks like a bug to me...please don't tell me I have to catch every row as it is databound to reformat these columns.

2 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 11 Nov 2008, 07:09 PM
Hello Joe,

Please examine the attached example based on the code-snippet you provided.

For your convenience I explained the matter below:

dt.Columns.Add("Date1"); 
dt.Columns.Add("Date2"typeof(DateTime)); 

dt.Rows.Add(i, DateTime.Now, DateTime.Now); 

Even though I'm supplying a correct values - DateTime, the DataFormatString property on Date1 column won't format this value.

<telerik:GridBoundColumn DataField="Date1" EmptyDataText="&amp;nbsp;" HeaderText="Date1" 
    SortExpression="Date1" UniqueName="Date1" DataFormatString="{0:DATE: dd/MM/yyyy}" DataType="System.DateTime" 
    HtmlEncode="false"
    <HeaderStyle Wrap="False" /> 
</telerik:GridBoundColumn> 

Additionally I set the DataType property of the GridBoundColumn to System.DateTime but this won't work also because the real type of my DataTable column is not DateTime (it's string by default).

Finally I want to mention that DataFormatString won't format MySqlDateTime type and this is not Telerik-related restriction. For instance this can be avoided when using SqlDataSource instead of MySql connector directly.

You can test the aforementioned statement with the following code (no Telerik controls involved):
MySqlDateTime mysqldate = new MySqlDateTime(DateTime.Today); 
DateTime netdate = DateTime.Today; 
string date1 = string.Format("{0:Year:yyyy}", mysqldate); 
string date2 = string.Format("{0:Year:yyyy}", netdate); 
 

I hope this information is helpful.

Kind regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
jfkrueger
Top achievements
Rank 1
answered on 11 Nov 2008, 08:41 PM
Ok, so the underlying field in the datasource has to be defined as a valid date type as opposed to the default of string.

Thanks!
Tags
Grid
Asked by
jfkrueger
Top achievements
Rank 1
Answers by
Daniel
Telerik team
jfkrueger
Top achievements
Rank 1
Share this question
or