ASP.NET GridView DateTime Sort

Posted on 2011-04-28
Last Modified: 2012-05-11
I have a gridview on an 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 29

    Accepted Solution

    LVL 40

    Expert Comment

    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
    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now