C# VB.Net Formatting DateTime Field in GridView in Asp.Net

Here i will discuss how you can Format Data in GridView Columns or Rows. Most of the time we require to render our GridView control by custom formatting since data source format does not contain our exact required formats. Like, you want to display DateTime as a Short Date instead of considering time portion from Database. In this post i will fully concentrate on “How to Format GridView Data for DateTime Datatype“.

Formatting Date Time in Bound Column of a GridView:

<asp:BoundField DataField="LastDelivery" HeaderText="Recent Delivery" DataFormatString = "{0:y}" HtmlEncode="false"></asp:BoundField>


<asp:BoundField DataField="LastDelivery" HeaderText="Recent Delivery" DataFormatString = "{0:MMMM yyyy}" HtmlEncode="false"></asp:BoundField>

Note: Don’t miss the HtmlEncode=”false” property.

Formatting Column Data of a GridView from Code Behind:
To format GridView data from run time or from code behind you can use GridView RowDataBound method like below:
C# Code:

    protected void gvEdit_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            e.Row.Cells[4].Text = Convert.ToDateTime(((DataRowView)e.Row.DataItem)["LastDelivery"]).ToString("y");

Note: Change your column name from [“LastDelivery”].

VB.Net Code:

    Protected Sub gvEdit_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(4).Text = Convert.ToDateTime((CType(e.Row.DataItem, DataRowView))("LastDelivery")).ToString("y")
        End If
    End Sub

Note: Change your column name from [“LastDelivery”].

An output should be:

List of Date Time Formats for GridView:

Format character Description Pattern Example
d Short date MM/dd/yyyy 12/1/2009
D Long date dddd, dd MMMM yyyy Sunday, April 12, 2009
t Short time HH:mm 5:12 PM
T Long time HH:mm:ss 5:12:00 PM
f Full date/time(short time) dddd, dd MMMM yyyy HH:mm Sunday, April 12, 2009 5:12 PM
F Full date/time(long time) dddd, dd MMMM yyyy HH:mm:ss Sunday, April 12, 2009 5:12:00 PM
g General date/time(short time) MM/dd/yyyy HH:mm 4/12/2009 5:12 PM
G General date/time(long time) MM/dd/yyyy HH:mm:ss 4/12/2009 5:12:00 PM
m or M Month day MMMM dd April 12
r or R GMT ddd, dd MMM yyyy HH’:’mm’:’ss ‘GMT’ Sun, 12 Apr 2009 7:12:00 GMT
s Sortable date/time yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss 2009-04-12T14:12:00
u UniversalSortableDateTimePattern using universal time yyyy’-‘MM’-‘dd HH’:’mm’:’ss’Z’ 2009-04-12 14:12:00z
U Full date and time (long date and long time) using universal time dddd, dd MMMM yyyy HH:mm:ss Sunday, April 12, 2009 10:12:00 PM
y or Y Year month MMMM yyyy April, 2009

What is HtmlEncode?
Microsoft introduced this new property to prevent cross site scripting (CSS) attacks. This way if there is any malicious text in the fields, it will get encoded and will not execute on client’s browser. If this property is set to true, the formatting implementation in the control first encoded the text using HttpUtility.HtmlEncode. And after that it calls String.Format method on the encoded text value. Where as when HtmlCode is set to false, the formatting is done on the data value itself. So the way to fix your problem with formatting would be to set HtmlEncode property to false.

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code