<asp:BoundField DataField="data"
      HeaderText="Data" SortExpression="data"
     DataFormatString="{0:d}" />

Why does this line of code (ehm... markup) always render the string "27/03/2007 0.00.00" as if it was just using a normal ToString(), instead of "27/03/2007", which is the result of the String.Format method using {0:d} (ShortDate format)?

At first I was thinking about some kind of error in the format string, but after carefully reading my format string bible I decided it was not my fault: a few search on Google and I found out this interesting post about some breaking changes in the ASP.NET 2.0 GridView.

The ASP.NET team decided to HtmlEncode by default all texts in order to prevent possible Cross-Site Scripting attacks. And for some strange reasons the HtmlEncode is applied before the Format method: to use the Format string now you need to add a HtmlEncode="false".

So, the correct definition for the bound field should be that one:

<asp:BoundField DataField="data"
 HeaderText="Data" SortExpression="data"
 DataFormatString="{0:d}" HtmlEncode="false" />

And the same applies to column headers: if you write HeaderText="Start<br>Date" and you don't say not to HtmlEncode it, then you will not see the 2 lines.

Bug or feature? This is the question...