Display date without time in ASP.NET Gridview using C#

If you want to display a date in ASP.NET Gridview without the time component, you can use the ToString() method with a custom format string. In C#, you can specify a custom format string by using the "d" format specifier. This will display the date in short date format, without the time component.

To apply this formatting to a date field in an ASP.NET Gridview, you can use the DataFormatString property of the BoundField or TemplateField controls. For example, if you have a date field named "OrderDate", you can set the DataFormatString property as follows:

<asp:BoundField DataField="OrderDate" DataFormatString="{0:d}" />

This will display the date in short date format (e.g. "6/15/2021"), without the time component.

If you're using a TemplateField control, you can use the <%# %> syntax to apply the formatting. For example:

<asp:TemplateField HeaderText="Order Date">
    <ItemTemplate>
        <%# Convert.ToDateTime(Eval("OrderDate")).ToString("d") %>
    </ItemTemplate>
</asp:TemplateField>

This will also display the date in short date format, without the time component.

By using the DataFormatString property or a custom format string, you can easily display dates in ASP.NET Gridview without the time component, making your application more user-friendly.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information