CODEDIGEST
Skip Navigation LinksHome » Article » ASP.Net Article » How to format DateTime in GridView BoundColumn and TemplateColumn?  Submit Articles and Win Geeky Prizes!!   You are not logged in.
Search
 

ASP.Net Web Hosting
MS SQL 2008 Hosting – Click Here
 

Product Spotlight
 

Technologies
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
Read more..

 
How to format DateTime in GridView BoundColumn and TemplateColumn?

By Satheesh babu
Posted On Sep 25,2008
Article Rating: (Login)
Be first to rate
this article.
No of Comments: 4
Category: ASP.Net
Print this article.

How to format DateTime in GridView BoundColumn and TemplateColumn?

 

Most often, we will get requirements to display date in GridView column. To format the date displayed in the GridView column, we can use “DataFormatString” to set the DateTime format in a Bound Column. This article will helps us formatting the date string in GridView column when we use Bound column and Template column. At the end of this article, we will have a list of date formatting string that can be used to format the date to different formats.

 

Format Date in Bound Column

Consider we have a GridView that displays Employee information with FirstName, LastName, Email and DOB, where the DOB is stored with time in database.

 

<asp:GridView ID="gvUsersBF" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#010101" BorderStyle="Groove" BorderWidth="1px" CellPadding="4">

                    <Columns>

                        <asp:BoundField DataField="Email" HeaderText="Email" ReadOnly="True" />

                        <asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="True" />

                        <asp:BoundField DataField="LastName" HeaderText="Last Name" ReadOnly="True" />

                        <asp:BoundField DataField="DOB" HeaderText="Date Of Birth" ReadOnly="True" />                                           

                    </Columns>

                    <FooterStyle BackColor="White" ForeColor="#330099" />

                    <RowStyle BackColor="White" ForeColor="#330099" />

                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />

                    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />

                    <HeaderStyle BackColor="#F06300" Font-Bold="True" ForeColor="#FFFFCC" />

        </asp:GridView>

 

To make the DOB column to display a short date format without the time, we need to include DataFormatString = "{0:d}"  for the BoundColumn to display a short date.

 

<asp:BoundField DataField="DOB" HeaderText="Date Of Birth" DataFormatString = "{0:d}"   ReadOnly="True" />           

 

Making the above setting only will not work. It is because, by default, HTML encode is turned “on” to prevent executing some dangerous script when injected through cross site scripting attacks. Because of this default setting, the text in the column will be encoded so that the cross site scripting is not executed. In order to make the above setting work, we need to make the HtmlEncode property of BoundColumn to false.

 

<asp:BoundField DataField="DOB" HeaderText="Date Of Birth" HtmlEncode="False" DataFormatString = "{0:d}"   ReadOnly="True" />           

                    

For Long Date,

<asp:BoundField DataField="DOB" HtmlEncode="False" DataFormatString = "{0:D}" HeaderText="Date Of Birth[Long Date]" ReadOnly="True" /> 

 

For Long Time,

<asp:BoundField DataField="DOB" HtmlEncode="False" DataFormatString = "{0:T}" HeaderText="Date Of Birth[Long Time]" ReadOnly="True" />


Hence, setting HtmlEncode="False" for the bound column will do the magic.

 

 

Sponsors

Article Contest - Winners

Winners of August, 2008



Similar Articles

Format Date in Template Column

Sometimes we will also use Template column to display the date inside a child control like Label.

 

<asp:GridView ID="gvUsersBF" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#010101" BorderStyle="Groove" BorderWidth="1px" CellPadding="4">

                    <Columns>

                        <asp:BoundField DataField="Email" HeaderText="Email" ReadOnly="True" />

                        <asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="True" />

                        <asp:BoundField DataField="LastName" HeaderText="Last Name" ReadOnly="True" />

<asp:TemplateField HeaderText="Date Of Birth[Short Date]">

                        <ItemTemplate>

                        <asp:Label ID="lblDOB" runat="server" Text='<%# Bind("DOB") %>'></asp:Label>

                        </ItemTemplate>

                        </asp:TemplateField>                         

 </Columns>

                    <FooterStyle BackColor="White" ForeColor="#330099" />

                    <RowStyle BackColor="White" ForeColor="#330099" />

                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />

                    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />

                    <HeaderStyle BackColor="#F06300" Font-Bold="True" ForeColor="#FFFFCC" />

        </asp:GridView>

 

To bind the database column value to the child control we either use Bind() or Eval() methods. If you see the above code, we have used Bind() to bind DOB to the text property of Label control.

 

Text='<%# Bind("DOB") %>'

 

Using Eval in the place of Bind will also work. You use the Eval method when the control will only display values. You use the Bind method when users can modify a data value—that is, for data-update scenarios. 

 

These binding methods can take the formatting string as the next argument to set the format of the date displayed.

 

<ItemTemplate>

                        <asp:Label ID="lblDOB" runat="server" Text='<%# Eval("DOB","{0:d}") %>'></asp:Label>

                        </ItemTemplate>

                        </asp:TemplateField>   

 

For Long Date,

                        <asp:TemplateField HeaderText="Date Of Birth[Long Date]">

                        <ItemTemplate>

                        <asp:Label ID="lblDOB" runat="server" Text='<%# Bind("DOB","{0:D}") %>'></asp:Label>

                        </ItemTemplate>

                         </asp:TemplateField>

 

For Long Time,

                       <asp:TemplateField HeaderText="Date Of Birth[Long Time]">

                        <ItemTemplate>

                        <asp:Label ID="lblDOB" runat="server" Text='<%# Bind("DOB","{0:T}") %>'></asp:Label>

                        </ItemTemplate>

                        </asp:TemplateField> 

   

 

The following table shows some date format strings that can be used to format date in GridView columns,

Format Pattern

Name

Example

d

Short date

11/8/2008

D

Long date

Sunday, August 11, 2008

t

Short time

3:32 PM

T

Long time

3:32:00 PM

f

Full date/time
(short time)

Sunday, August 11, 2008 3:32 PM

F

Full date/time
(long time)

Sunday, August 11, 2008 3:32:00 PM

g

General date/time
(short time)

8/11/2008 3:32 PM

G

General date/time
(long time)

8/11/2008 3:32:00 PM

m or M

Month day

August 11

r or R

RFC 1123

Sun, 11 Aug 2008 8:32:00 GMT

s

Sortable date/time

2008-08-11T15:32:00

u

Universable sortable date/time

2008-08-11 15:32:00z

U

Universable sortable date/time

Sunday, August 11, 2008 11:32:00 PM

y or Y

Year month

August, 2008

 

 

Downloads

Source Code 

 

Conclusion

The databound controls like GridView are extensively used in web applications to display data. We will always have requirement to format the data that is displayed, most of the time formatting the date time strings. This article will help us to format the date into many useful formats and it can be used as a future reference for formatting date time strings.

Happy Coding!!

 

Article Feedback
Title  
Submitted By  
Comment  
Enter the verification number
 
Comments
Good
Very neatly explained and
and the required formats neatly presented in a table ...
good for a quick reference
GREAT
Very nice code. But i how can i add date time contorl here.
Good!!
Thanks for the info!
Thanks
Very Nice!