A personal repository of technical notes. - CSC

DataFormatString Not Working

Problem
GridView BoundField DataFormatString not working.

Solution
Specify HtmlEncode="false".

<asp:boundfield datafield="MyDataColumn" headertext="Col Hdr" DataFormatString="{0:p}" HtmlEncode="false"/>

See Microsoft note:
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.
References
BoundField.DataFormatString Property (System.Web.UI.WebControls)
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring(VS.80).aspx

BoundField.HtmlEncode Property (System.Web.UI.WebControls)
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.htmlencode(VS.80).aspx

No comments:

Post a Comment