Here I will explain how to set date format in gridview using asp.net
Description:
In previous post I explained how to convert datetime to date in sql server . Now I will explain how to format date in gridview. In many situations we will bind date and currency values to gridivew at that time our gridview is something like this
![]() |
Character | Description | Date Format | Example |
Y or y | Month and year | MMMM,YYYY | May 2011 |
d | Short Date | MM/dd/yyyy | 15/1/2011 |
D | Long Date | dddd,MMMM dd,yyyy | Sunday, May 15, 2011 |
f | Full date and time without secs | dddd,MMMM dd,yyyy HH:mm | Sunday, May 15, 2011 4:23PM |
F | Full date | dddd,MMMM dd,yyyy HH:mm:ss | Sunday, May 15, 2011 4:23:45PM |
g | Date time without sec | MM/dd/yyyy HH:mm | 5/15/2011 4.25 PM |
G | Full date | MM/dd/yyyy HH:mm:ss | 5/15/2011 4.25:35 PM |
M or m | Month and day | MMMM dd | May 15 |
R or r | Full date with GMT | ddd,dd MMMM yyyy HH:mm:ss GMT | Sun,15 May 2011 4.25:35 GMT |
s | Sortable Date time | yyyy-MM-ddTHH:mm:ss | 2011-05-15T16:33:55 |
t | Short time | HH:mm | 4:56 PM |
T | Long Time | HH:mm:ss | 4:56:45 PM |
u | Sortable DateTime Pattern using universal time | yyyy-MM-ddTHH:mm:ssz | 2011-05-15T16:33:55z |
U | Long Date | ddd,dd MMMM yyyy HH:mm:ss | Sunday, May 15, 2011 4:54:10 PM |
C | Currency Format | $00.00 | $10.00 |
D | Decimal Format | 10 | 10 |
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Gridvew Date format</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView runat="server" ID="gvdetails" DataSourceID="dsdetails" AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false"> <RowStyle BackColor="#EFF3FB" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:BoundField DataField="Date1" HeaderText="Date1" HtmlEncode="false" DataFormatString="{0:s}" /> <asp:BoundField DataField="Date2" HeaderText="Date2" HtmlEncode="false" DataFormatString="{0:D}" /> <asp:BoundField DataField="Date3" HeaderText="Date3" HtmlEncode="false" DataFormatString="{0:m}" /> <asp:BoundField DataField="Date4" HeaderText="Date4" HtmlEncode="false" DataFormatString="{0:d}" /> <asp:BoundField DataField="Total" HeaderText="Total" HtmlEncode="false" DataFormatString="{0:C2}" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="dsdetails" runat="server" SelectCommand="select * from DateFormat" ConnectionString="<%$ConnectionStrings:dbconnection %>"></asp:SqlDataSource> </div> </form> </body> </html> |
<connectionStrings> <add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/> </connectionStrings > |
![]() |
<asp:TemplateField HeaderText="Date1"> <ItemTemplate> <asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date1","{0:y}") %>'/> </ItemTemplate> </asp:TemplateField> |
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|


Subscribe by RSS
Subscribe by Email
12 comments :
<%# Bind("Data_DateField", "{0:dd/MMM/yyyy}"%>
please send me how to do the projects in .net.. am fresher in dotnet.. please give me the every thing of projects by using web pages..
Regards,
Jyothsna.k
i cant find these lines in my html code... i am connecting to sql thro code behind...then how can i modify the above code....plz do reply for this..
i am fresher to dotnet...plz help me..
thanks
karthik
i cant find sql datasource ID lines in my html code... i am connecting to sql thro code behind...then how can i modify the above code....plz do reply for this..
i am fresher to dotnet...plz help me..
thanks
karthik
Thank You Suresh
thanks
great effort
it solves my problem
Thank you ..
It solved my Problem..
Great and easy line of code
thnx its realy helpful
Thank you and keep posting...
just needed that