DataFormatString not working with BoundFields

DataFormatString not working with BoundFields

April 4th, 2006 // 9:04 am @

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

Leave a Reply

Latest Posts

Testimonials

"Amar clearly has a thorough knowledge of Sharepoint, this knowledge, coupled with his professionalism and dedication made him a tremendous asset to the project. He was the key contributor to the later stages of the project delivery effort (the really difficult bit, long hours and hard work that puts huge demands on people), and I can state categorically that without his focussed dedication and hard work we would not have been able to meet the timescales imposed upon the project."

Tim Ellis , Sharepoint Project Manager , Royal Bank of Scotland

Subscribe Now