Getting the DataFormatString to work with a GridView

Posted Thursday, January 26, 2006 9:59 AM by C-Dog's .NET Tip of the Day

Sooner or later you will probably use a GridView to do some simple data binding. The GridView is great because once you set up your SqlDataSource or ObjectDataSource (or whatever), it automatically detects the schema of your table. This is great because it allows you to databinding really easily.

There are also a number of features that let you format the data you have without writing in code. One such propery is the DataFormatString property. This property is useful for formating numbers or dates. If you are trying to format a date however, you may notice that the string you are providing is not doing anything. It took me a while to figure this out but basically this is because of HTML Encoding. In the example below, I have a data format string of {0:MM/dd/yyyy} to format my date. By default, the HtmlEncode property is set to true. To get your DataFormatString simply change it to false as shown in the image below.

Read the complete post at