ASP.NET GridView DateTime Sort

Posted on 2011-04-28
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.
Question by:robtroller
LVL 30

Accepted Solution

anarki_jimbel earned 2000 total points
ID: 35487875
LVL 40

Expert Comment

ID: 35487951
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
LVL 16

Expert Comment

by:Kamal Khaleefa
ID: 35695825
in sql u can write this query

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

also on gridview level im using this format


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question