I'm currently building an application that uses GridView controls to display data to a page.
The DataSource for these GridViews are DataTable objects.
I wish to control the format in which the DateTime fields are displayed in a format like: "June 16, 2006"
I initially tried doing it in the SQL-Server stored procedure that retrieves the data however that returns a varchar data type which breaks the sorting functionality of the grid view because now my DateTime field sorting is done alphanetical instead of chronologically.
My initial thoughts are to create a new DataColumn in my DataTable and set an appropriate expression to create the date string for display but
looking on MSDN there doesn't appear to be any datetime functions to use in DataColumn.Expression.
How have others achieved this?
Many Thanks
"There is no such thing as a bug, just an unexpected feature."
What is the data type of this column (ClientDueDate)
in database? It seems that it is like a string field instead of a DateTime field. If this is the case, you need to fix the datatype first then you can get the right sorting order without changing anything on the gridview.
I'm not sure exactly what your question is. Are you asking how you can format your string if your using a Label in a TemplateField instead of using a BoundField? If so, you can use the overloaded signature of the Eval method to accomplish this like so:
Superfly1611
Participant
1476 Points
317 Posts
DateTime Format in gridview colum
Jun 21, 2006 10:41 AM|LINK
I'm currently building an application that uses GridView controls to display data to a page.
The DataSource for these GridViews are DataTable objects.
I wish to control the format in which the DateTime fields are displayed in a format like: "June 16, 2006"
I initially tried doing it in the SQL-Server stored procedure that retrieves the data however that returns a varchar data type which breaks the sorting functionality of the grid view because now my DateTime field sorting is done alphanetical instead of chronologically.
My initial thoughts are to create a new DataColumn in my DataTable and set an appropriate expression to create the date string for display but looking on MSDN there doesn't appear to be any datetime functions to use in DataColumn.Expression.
How have others achieved this?
Many Thanks
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: DateTime Format in gridview colum
Jun 21, 2006 12:22 PM|LINK
Sure, if you're using a BoundField, simply do as follows:
<asp:boundfield datafield="Your_Date_Column" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" />Just make sure you set the HtmlEncode attribute to False or it won't work.Microsoft MVP - ASP.NET
Superfly1611
Participant
1476 Points
317 Posts
Re: DateTime Format in gridview colum
Jun 21, 2006 12:34 PM|LINK
ReyN
Contributor
2148 Points
413 Posts
Re: DateTime Format in gridview colum
Jun 21, 2006 12:36 PM|LINK
and here's a list of the different DateTime formats you can apply with the DataFormatString
Date and Time Format Patterns [:D]
aspxtreme
Superfly1611
Participant
1476 Points
317 Posts
Re: DateTime Format in gridview colum
Jun 21, 2006 12:50 PM|LINK
mo.baig
Member
2 Points
1 Post
Re: DateTime Format in gridview colum
Jan 31, 2007 03:56 PM|LINK
I am using:
<
asp:BoundField HeaderText="Due Date" DataField="ClientDueDate" SortExpression="ClientDueDate" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false" > <ItemStyle Width="5%" HorizontalAlign="Center" /> <HeaderStyle Wrap="True" /> </asp:BoundField>and when i sort the column of the GridView, I get this:
which is not sorted properly...
any suggestions ??
limno
All-Star
117314 Points
7997 Posts
Moderator
MVP
Re: DateTime Format in gridview colum
Feb 01, 2007 04:01 AM|LINK
Format your SQL query with instant sql formatter:
http://www.dpriver.com/pp/sqlformat.htm
rmc7777
Member
52 Points
44 Posts
Re: DateTime Format in gridview colum
Aug 15, 2007 09:12 PM|LINK
This was a great help for me too. Thanks.
LacOniC
Member
333 Points
93 Posts
Re: DateTime Format in gridview colum
Oct 08, 2007 06:08 PM|LINK
If it's not boundfield, a label, what to do?
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: DateTime Format in gridview colum
Oct 08, 2007 06:37 PM|LINK
I'm not sure exactly what your question is. Are you asking how you can format your string if your using a Label in a TemplateField instead of using a BoundField? If so, you can use the overloaded signature of the Eval method to accomplish this like so:
<asp:templatefield headertext="Color"> <itemtemplate> <asp:label id="lblDate" runat="server" text='<%# Eval("Your_Date_Column", "{0:MMMM d, yyyy}") %>' /> </itemtemplate> </asp:templatefield>Microsoft MVP - ASP.NET