Mervin Johnsingh Mervin Johnsingh - 2 months ago 9
ASP.NET (C#) Question

How can you show the date in a gridview as a date only instead of a datetime?

I am pulling in date values from a sql server database using a gridview and and the date gets converted from


12/12/2009 to 12/12/2009 12:00:00 AM


How do I prevent that from happening ?

Thanks !

Answer

You can use the ToString() method with a mask:

ToString("MM/dd/yyyy");

UPDATE: Just realized it would be easier in your case to do this in the grid view template

<asp:BoundField DataField="MyDate" DataFormatString="{0:MM/dd/yyyy}" />