Thursday, October 29, 2009

GridView BoundColumn Date Format

aWhen using dates in GridView bound columns, It displays without any specified format. The date with time will display as a long length text. We can set the format what we need, use DataFormatString attribute with the BoundField column.

If we want to display the date as 25/12/2005 give DataFormatString="{0:dd/MM/yyyy}"
and also we have to set the HtmlEncode="False" to set the field value is not Html-encoded.

Example



... HtmlEncode="False" DataFormatString="{0:dd/MM/yyyy}"
DataField="recdate" HeaderText="CREATED DATE">"



For TemplateField, use the same format string within the Eval method of  Template (May be ItemTemplate, EditItemTemplate, etc...) as the second argument

 Example

       Eval("recdate","{0:dd/MM/yyyy}")