Search This Blog

Date format in a GridView's bound field

posted on Sunday, March 11, 2012


When working with GridViews and bound fields, you will notice that all values will be displayed in their default formats: including dates.

As you can see in the "before"-image below, for dates this is a long date format. To fix this and set the date to whatever format you like.

You should set the "DataFormatString" and "HtmlEncode" settings of the bound field. Go ahead and set the DataFormatString like this: "{0:dd/MM/yyyy}" and don't forget to set HtmlEncode to false to allow the format to be parsed. In the example below you see how this is done to display the dates in a shorter format.
<asp:boundfield datafield="EindPublicatie" dataformatstring="{0:dd/MM/yyyy}" headertext="Solliciteren tot" htmlencode="False">
   <itemstyle width="100px">
     <headerstyle cssclass="HeaderStyle"></headerstyle>
   </itemstyle>
</asp:boundfield>
Before our changes:


After our changes:

Through the link below you can find more formats (specificlly date formats and numeric formats):
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx

Could be useful, right?

No comments:

Post a Comment