How To Set a Date Format In GridView Using ASP.NET or How to format datetime in gridview boundfield and template columns
How To Set a Date Format In GridView Using ASP.NET or How to format datetime in gridview boundfield and template columns
Introduction:
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
In
some situations we don’t want to show complete date and time we need to
display only date or only time or we need to display date in different
formats and we need to change the currency format display also. To
display date with different formats in gridview we need to set DataFormatString and HtmlEncode properties.
Here is the list of date formats for gridview.
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
|
Now
we will implement these formats in our gridview for this first bind the
date values from your database and design your aspx page like this
<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>
|
After that set your database connection in web.config like this
<connectionStrings>
<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings >
|
Demo
If you want to use these date string formats in itemtemplate field we need to bind fields like this
<asp:TemplateField HeaderText="Date1">
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date1","{0:y}") %>'/>
</ItemTemplate>
</asp:TemplateField>
|
Comments
Post a Comment