I’m filling a GridView from a DataTable. I would like to have just the date part of the datetime fields to show.
I have used :
CONVERT(VARCHAR(10),MyDatetime,103) AS 'DateOnly'
This displays fine but sorts ‘incorrectly’ (it is treated as a string rather then a date)
The simple solution I want to avoid would be
CONVERT(VARCHAR(10),MyDatetime,102) AS 'DateOnly'
i.e. have the year first, this would sort correctly but isn’t quite right.
I’ve also tried:
CONVERT(date,MyDatetime,103) AS 'DateOnly' and
DATEADD(D, 0, DATEDIFF(D, 0, MyDatetime)) AS 'DateOnly'
These look fine in sql server, but when rendered in the GridView add on the time 00:00:00 after the date. – on the plus side the sort works fine.
The columns in the DataTable can vary so the columns are generated at run time and I can not use any solutions that involve editing the field like this:
<asp:BoundField DataField="DataFieldName" DataFormatString="{0:d}"></asp:BoundField>
I feel like I’m almost there but I just cant find the last piece to the puzzle.
I finally did it on RowDataBound like this: stackoverflow.com/questions/4249629/formatting-dynamic-gridview
I’m sure there must be a better solution but I couldn’t find it.