DataFormatString of GridView control is not working and the columns don’t get formatted


Recently, I found an issue with the Gridview control. I was adding some formatted columns and no matter how much I tried it wouldn’t show up as expected. Similar code seems to work fine in Datagrid of VS 2003. One of the columns which I had was supposed to use the following formatting “{0:#,###}” and it simply didn’t work.


After some investigation I found that it was a problem due to HTMLEncoding. For more explanation you have view http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=b00b3ec6-e8fa-4f4d-b1e9-39162abc847b. All you need to do is to set the BoundColumn’s HtmlEncode property to False.


To see how I formatted the GridView dynamically, visit the following link http://blogs.msdn.com/rahulso/archive/2006/02/28/540839.aspx. Try commenting out the line which says, bndColumn.HtmlEncode = False and see the effect.


To know more about BoundField.DataFormatString Property visit http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx


Hope that helps!

Comments (8)

  1. Brent says:

    Thank you!!! I’ve been searching for an answer to this issue for a while now.

  2. Eric says:

    Thank You!

  3. John says:

    Awesome – could not find the cure.  Thanks!!!!

  4. SonDH says:

    Thank you very much!

  5. csd says:

    <html>

    <body>Anand</body></html>

  6. sachin pawar says:

    HtmlEncode="false" set this property of the particular column for which you want to add dataformatstring..

  7. Sue says:

    Thank you so much. By the way how did you find it?

  8. No Problems Sue! Being a troubleshooter at MS enlightens you with a lot of tricks, I guess!

    Mainly, I started by commenting some lines which I suspected and pretty soon figured out the culprit. I wanted to file a bug, but found that it is already filed and documented here…

    http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=b00b3ec6-e8fa-4f4d-b1e9-39162abc847b