Monday, August 17, 2009

Change DateFormat In an ASP.NET GridView

While displaying dates in the GridView, a very common requirement is to display the date format in a user friendly manner. By default, the HTMLEncode property of a BoundField is set to True, which makes it difficult to format the date.

Note: HTMLEncode=True helps prevent cross-site scripting attacks.

In order to display the date in a user friendly date format, use the following method to turn off the HTMLEncode and use :



DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True">




HtmlEncode="false" HeaderText="SomeDate" />

...







If you do not want to do it this way, the other way is to use a Template Field as shown below



DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True">














...



No comments:

Post a Comment