How To Set a Date Format In GridView Using ASP.NET 2.0

Posted In Asp.net - By admin On Tuesday, July 28th, 2009 With 1 Comment

A very common desire is to set a column of a gridview to display just the month, day and year of a DateTime type.The problem is the by default, the HtmlEncode property of the boundfield attribute is set to True

<asp :GridView ID=“GridView1″ runat=“server”>
<columns>
<asp :BoundField DataField=“DOB”
DataFormatString=“{0:M-dd-yyyy}”
HtmlEncode =“True” HeaderText=“Date of Birth” />
</columns>
</asp>

The second choice is to make the column a template and simply set the format string directly in the Label ,Text Fields or bind data direct as follows.

<asp :GridView ID=“GridView1″ runat=“server”>
<columns>

<%# Eval(“DOB”, “{0:M-dd-yyyy}”) %></columns>
</asp>

About -

Displaying 1 Comments
Have Your Say

  1. Niraj says:

    Please explain in more details

    Thanks….!

    [Reply]

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>