How can I stop the time from displaying in a GridView when displaying a date?

I have a web application that I am working on(ASP.NET 2.0 & C#). In it, I have a gridview that gets data from an Oracle Database. Some of the data that I need to display is dates. But when the dates in my gridview show like this:

2009-04-02 00:00:00

I’d prefer they show without the time. I’m using the codebehind to get the data, so I cant alter the fields of the gridview manually. This is all the code for my gridview :

<asp:GridView ID="Grid" runat="server" EmptyDataText="There are no data records to display." ></asp:GridView>

How do I stop the time from displaying?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

if you set your datasource programmatically (code behind), you can still have all your columns in the gridview (use asp:DataBoundColumn)

DataFormatString="{0:d}"

EDIT

this code is for your aspx page, if you really want to stick your presentation logic in your code behind – you have 2 options:

  1. format the Date on “RowDataBound” event
  2. format the Date in your query – that way you won’t have to deal with formatting in code at all

Method 2

You can apply a dataformatstring in the gridview column to only show the date.

To get it to work you must make sure that HTMLEncode is set to false.

More information

Method 3

You can use
DataFormatString=”{0:d}” HtmlEncode=”False”

do not forget to use the last section [htmlencode]

I hope this works … this works for me !!!!

Arunendra


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x