DataFormatString not working with BoundFields
April 4th, 2006 // 9:04 am @ Amar
I came across this issues when I added a GridView to display some data. One of the data fields was a Date Time field and it was displaying both the date and time portions. So I added the DataFormatString as:
<asp:BoundField DataField=”PubDate” HeaderText=”PubDate” SortExpression=”PubDate” DataFormatString=”{0:d}” >But strangely, it did not display. It was not using any formatting which I put in the DataFormatString. The solution to this was to set HtmlEncode=false. This made the control honor the DataFormatString. The explaination to this behaviour can be found in the MSDN Documentation for BoundField. The exceprt of this behaviour is given below.
When the HtmlEncode property is true, the value of the field is HTML encoded to its string representation before the formatting string is applied. For some objects, such as dates, you might want to control how the object is displayed with a formatting string. In those cases, you must set the HtmlEncode property to false.
Category : dotNet