Formatting the date display

Formatting the date display

In order to format the date display in an ASP.NET web page, you can use the DataFormatString property of the BoundField or TemplateField in the GridView control.

Here’s an example of how to use it:

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” DataSourceID=”SqlDataSource1″>


        <asp:BoundField DataField=”Date” HeaderText=”Date” DataFormatString=”{0:d}” />

        <asp:BoundField DataField=”Description” HeaderText=”Description” />

        <asp:BoundField DataField=”Amount” HeaderText=”Amount” DataFormatString=”{0:C}” />



In this example, the DataFormatString property is set to {0:d} for the “Date” column and {0:C} for the “Amount” column. The d format specifier is used to display the date in a short date format, while the C format specifier is used to display the amount in a currency format.

You can use other format specifiers as well, such as g for a short date and time format, t for a short time format, and yyyy-MM-dd for a custom date format. Note that the format specifier is enclosed in braces {} and includes a placeholder {0} for the value to be formatted. The value will be replaced with the actual value from the data source.

Apply for ASP.NET Certification Now!!

Back to Tutorial

Get industry recognized certification – Contact us