We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


ASP.NET GridView DateTime Sort

robtroller asked
Medium Priority
Last Modified: 2012-05-11
I have a gridview on an ASP.net page that has a column bound to a datetime fiield.  I was converting the value on the select to display the date as 0/14/1964 but found the sort was looking at the values as a string and sorting but month.  After reading I have set the data format string to {0:yyyy/MM/dd} and the hemlencode property to False.  The dates now display as 1964/01/14.  I anticipated the sort would now sort the values correctly, instead it sets them back to 1/14/1964 and sorts by month again.  I''m mystified!  Any help would be appreciated.
Watch Question

Senior Developer
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Top Expert 2009

anarki's link above will show you the solution, but the short answer is that the result set is being ordered prior to the field formatting.

You can order it in your SQL, there is no requirement that you order by the same expression as you use to query the result.

select foo from tab order by bar
Kamal KhaleefaInformation Security Specialist

in sql u can write this query

select convert(nvarchar(10),YourField,20) from ur table

also on gridview level im using this format

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.