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. 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.

   
      
   

Before:


After:

Here you find more formats (dates and numeric):
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx

Could be useful, right?

No comments:

Post a Comment