More in the line of stupid pet tricks: I keep running into this particular issue from time to time and promptly forget the solution, so I’m blogging it again for my own sanity.
If you use GridView and you have a BoundField column in you might run code like this to format a date string:
<asp:BoundField DataField="OrderDate" DataFormatString="{0:d}" HeaderText="Date"
meta:resourcekey="BoundField">
<headerstyle font-italic="False" font-overline="False" font-strikeout="False"
font-underline="False" horizontalalign="Center" width="200px"/>
</asp:BoundField>
The {0:d} is supposed to format the date as a short date string. Instead if you run this you’ll get the default time string.
To make this work you have to add:
<asp:BoundField DataField="OrderDate" DataFormatString="{0:d}" HeaderText="Date"
meta:resourcekey="BoundField"
HtmlEncode="false">
<headerstyle font-italic="False" font-overline="False" font-strikeout="False"
font-underline="False" horizontalalign="Center" width="200px"/>
</asp:BoundField>
Real intuitive ain’t it?
According to MSDN:
|
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. |
Cute.
Anyway, the fix is easy enough once you know what you’re looking for.
Until next time when I get to it 3 months from now and I will have forgotten this unobvious piece of trivia.