aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 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

Here I will explain how to set date format in gridview using


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.

Date Format
Y or y
Month and year
May 2011
Short Date
Long Date
dddd,MMMM dd,yyyy
Sunday, May 15, 2011
Full date and time without secs
dddd,MMMM dd,yyyy HH:mm
Sunday, May 15, 2011 4:23PM
Full date
dddd,MMMM dd,yyyy HH:mm:ss
Sunday, May 15, 2011 4:23:45PM
Date time without sec
MM/dd/yyyy HH:mm
5/15/2011 4.25 PM
Full date
MM/dd/yyyy HH:mm:ss
5/15/2011 4.25:35 PM
M or m
Month and day
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
Sortable Date time
Short time
4:56 PM
Long Time
4:56:45 PM
Sortable DateTime Pattern using universal time
Long Date
ddd,dd MMMM yyyy HH:mm:ss
Sunday, May 15, 2011 4:54:10 PM
Currency Format
Decimal Format

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="">
<head runat="server">
<title>Gridvew Date format</title>
<form id="form1" runat="server">
<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" />
<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}" />
<asp:SqlDataSource ID="dsdetails" runat="server" SelectCommand="select * from DateFormat" ConnectionString="<%$ConnectionStrings:dbconnection %>"></asp:SqlDataSource>
After that set your database connection in web.config like this

<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings >

If you want to use these date string formats in itemtemplate field we need to bind fields like this

<asp:TemplateField HeaderText="Date1">
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date1","{0:y}") %>'/>
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,, 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


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..


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..


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..


Madhu Mangalagiri said...

Thank You Suresh

Anonymous said...


prakash bhatt said...

great effort
it solves my problem

Unknown 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...


Anonymous said...


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 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.