Friday, October 12, 2007

DataFormatString Time and Date

So you have abound control, and want to only display the time portion? Or just the Date? Well, to start, that bound control must have HtmlEncode="false". Now, the only warning that I've seen on it, is that it maybe subject to an injection attack. And if you're worried about that, then you should probably use a templateitem instead.

Once you have the HtmlEncode="false" assigned to the bound item, use the DataFormatString="{0:t}" to get just the short time, and DataFormatString="{0:d}" to get a short date. You should be able to use any number of the formats in here to format it anyway you like. It took me some time to find these seemingly steps to get just the time on a datagrid column.

Mike