Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

How To Set a Date Format In GridView Using ASP.NET or How to format datetime in gridview boundfield and template columns

May 15, 2011
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>
I hope it helps you to format date in gridview
 

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 RSS subscribe by email Subscribe by Email

25 comments :

Anonymous said...

<%# Bind("Data_DateField", "{0:dd/MMM/yyyy}"%>

jyothsna Jessi said...

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

karthik said...

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

karthik said...
This comment has been removed by the author.
karthik said...

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

Madhu Mangalagiri said...

Thank You Suresh

Anonymous said...

thanks

prakash bhatt said...

great effort
it solves my problem

Anonymous said...

Thank you ..
It solved my Problem..
Great and easy line of code

Unknown said...

thnx its realy helpful

Anonymous said...

Thank you and keep posting...

Anonymous said...

just needed that

Anonymous said...

Hai i'am fresher for asp .net ,
please send me how to redirect pages based on the if condition whenever click the grid view cells.

Thanks in Advance

Anonymous said...

explanation ability for express a problem and provide its elegant solution u r write a suiltable code.

Anonymous said...

suresh sir i need in this format dd/mmm/yyyy (13-Aug-2013) or (13/Aug/2013) how can i do

Anonymous said...

Mr. Suresh, I need to check if date field in gridview is null coz i'm getting an error "FormatException was unhandled by user code: String was not recognized as a valid DateTime." I'm trying to do some conditional formating on the gridview on RowDataBound. Please help. Thx.

Anonymous said...

Awsome brother.........great post....thanks a lot

dhakechahitesh said...

great

Anonymous said...

good

Unknown said...

how to update date from front end to backend(sql server 2008)
when we run our grid view then edit date of birth through gridview to sql server 2008

Anonymous said...

Thanks a lot ✌️�� awesome and very simple to understand..

Anonymous said...

Thanks dude.... Really helpful....

Revathi said...

valuable code that was.....made my work simple...

Unknown said...

ramesh sir how to date increment sevendays using gridview

Unknown said...

thank u so much sir its really work for me....................

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.