views:

3299

answers:

5

I have a column that contains dates, when I click the column header the column is sorted numerically and not by date. How would I sort this by date? The date is in the format dd/mm/yy.

Example (sorted oldest first):

10/12/08 <--December 10/09/08 <--September 12/12/08 <--December

Many thanks

+5  A: 

Is the source a datatable? If so, you'll probably need to specify that the column in question is of type DateTime:

myDataTable.Columns["ColumnName"].DataType = System.Type.GetType("System.Date");

Hope that helps!

Gunny
A: 

This worked great, thanks.

myDataTable.Columns["ColumnName"].DataType = System.Type.GetType("System.Date");

Jayne, you should accept Gunny's answer if it solved your problem. Just click the little checkmark beside it.
Michael Haren
A: 

When I tried this I got a exception where System.Date is null..... I tried changing the type to System.DateTime and it displays both Date and Time. How can this be resolved?

anonymous
+1  A: 

You should use cell format

dgv.Rows[rowId].Cells["colCreated"].Style.Format = "HH:mm :: dd/MM/yyyy"; or something like this for the whole column (see DefaultCellStyle for column)

stepd
A: 

If the data source is from a stored procedure, return the date as two columns in the dataset. One is formatted (varchar) and another is in the Date time data type.

Say the stored procedure returns the colums : COLUMN_STRING_FORMAT and COLUMN_DATETIME_FORMAT

In the markup for grid,

<asp:BoundColumn DataField="COLUMN_STRING_FORMAT" SortExpression="COLUMN_DATETIME_FORMAT" DataFormatString="{0:dd-MMM-yyyy}" /> <asp:BoundColumn DataField="COLUMN_DATETIME_FORMAT" Visible="false"/>

Note the Sort Expression in the first line. It refers to the column in DateTime data type.

This worked for me when I was sorting on date in DD-MMM-YYYY format.

Jaye