DataFormatString won’t be applied

If you are looking for an answer why DataFormatString won’t be applied at your BoundField, the most reasonable answer is “It’s a bug”. A bug for which nobody at Microsoft seems to be responsible for.

In few words, if you have a BoundField object bound to a field of type DateTime (or whatever you want) with a DataFormatString like this:

when you will run your web application, the output instead to be as everybody would expect (MM/dd/yyyy) appears to be formatted using its ToString() method like so:

07/02/2007 7:35:54 PM

The problem is reconducible to an effort of preventing cross site scripting attacks, so the field value is HtmlEncoded before the new formmating is applied. This internally changes the output that doesn’t match anymore to a suitable Date format and for this reason the format requested won’t apply.

The resolution is to set up the HtmlEncode to false like so:

Technorati Tags: ,