Home DotNet Gridview Date Format
Gridview Date Format

It is very common that we need to format the date field in the gridview and we can do this through the use of the DataFormatString attributes for date format.

Examples:

<asp :GridView ID="GridView1" runat="server">

<columns>

  <asp :BoundField DataField="UpdatedOn"

     DataFormatString="{0:M-dd-yyyy}"

     HtmlEncode="false"

     HeaderText="Updated On"    />

</columns>

</asp>

You will need to set the HtmlEncode to false otherwise you will not get the expected result.  By default HtmlEncode is set to true to prevent cross-site script attacks and any unwanted content to be shown on the screen. When possible we should always have HtmlEncode set to true.

If you are using a template field then you would set it like this:

<asp :GridView ID="gvResult" runat="server"  >

 <columns>

  <asp :TemplateField HeaderText="Updated On" >

   <edititemtemplate>

    <asp :Label ID="lblUpdatedOn" runat="server"

      Text='<%# Eval("UpdatedOn", "{0:M-dd-yyyy}") %>'>

    </asp>

   </edititemtemplate>

   <itemtemplate>

    <asp :Label ID="lblUpdatedOn" runat="server"

      Text='<%# Bind("UpdatedOn", "{0:M-dd-yyyy}") %>'>

    </asp>

   </itemtemplate>

  </asp>

 </columns>

</asp>

 

 



Comments (0)
Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:D:angry::angry-red::evil::idea::love::x:no-comments::ooo::pirate::?::(
:sleep::););)):0
Security
Please input the anti-spam code that you can read in the image.

"