Click here to Skip to main content
15,924,507 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting a datatable with columns containg date . The date is displayed like 05-may-2015. I want sort it . I tried .But i am getting the day value only sorted . How solve this.. Please reply .........Thanks
Posted

Start by looking at your data source: the chances are that you are storing your date values in strings (or returning them in strings when t=you fetch them into your datatable) instead of using the correct datatype (DateTime for C#, DATETIME for SQL).
As a result, the comparison which is used for sorting is string based - which stops at the first distinct character and uses that as the result for the whole comparison.
So "12-April-2015" will be before "12-February-1999" because 'A' is before 'F' - teh erst of the string is ignored.

Sort out your data source! And if it's a Database, never store date or numeric values in strings - it always causes major problems when you try to use the actual data!
 
Share this answer
 
try this

XML
table.Columns.Add("dateValue", typeof(DateTime?));

   var orderedRows = from row in dt.AsEnumerable()
                     orderby  row.Field<DateTime>("Date")
                     select row;
   DataTable tblOrdered = orderedRows.CopyToDataTable();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900