DataGridView Soring

I really like the new DataGridView control, however, it always seems to sort columns containing numbers and dates as if they were strings.  The Windows Forms 1.x ListView control did the same thing so what I ended up doing was to create an class that implements the IComparer interface.  How do we handle this in DataGridView

Answer this question

DataGridView Soring

  • cjweis

    So, I guess I am at a loss with what to do here. My application has dynamic bound datasources depending on choices in the form, as well as where it was launched from. (i.e. different stored procedures as the source) So creating separate datasource procedures for every sort possibility is not an option for me. I don't understand why this functionality was removed from both the datagridview and datagrid controls.....

    Should I be using a different control here I really like all the other features of the datagridview... but without the smart sorting, it won't be of much use to me. Is there another type available, or plans to add the smart sorting into the control

    Thanks!


    Rob


  • JackG

    When databound the DataGridView does not perform any code for sorting but asks the datasource to sort. When unbound the DataGridView performs the sort and it doesn't have any logic to perform custom "smart" sorting of strings.

    -mark

    DataGridView Program Manager

    Microsoft

    This post is provided "as-is"


  • Perry Quest

    If that's the case, then I am totally lost. (Which is easy as I am a noob)

    I have a bound datagridview that insists on sorting everything as if it were a string. In VS.net my datagrids worked just fine using the 'd' format. After upgrading to VS 2005, neither the datagrid, or the new datagridview control sorts numbers and dates correctly, regardless of the format I specify.

    Please help!


  • rss245

    The only option for the DataGridview in to use it in an unbound mode and create a custom IComparer class that performs smart sort. There are no plans to change how the DataGridView sorts when databound.

    -mark

    DataGridView Program Manager

    Microsoft

    This post is provided "as-is"


  • jdavis

    I'm having the very same problem. I'm building a business applicationon top of the CSLA framework. Basically, this means I'm binding a custom collection to the datagridview. Everything works fine except date sorting. DataGridView always sorts them like strings even though the column is of type DateTime. Any ideas what to do here

  • JAWSORION

    Ok, I'll take a look into the Icomparer then.

    Thanks!


  • Melvis

    The grid will sort based upon the value type contained in the cell. For example, if you are storing date/time values you can set the column's ValueType to be of type DateTime and then the grid will sort by that. If you are actually using strings then you'll need to create a custom IComparer that performs smart string sorting.

    Hope this helps!
    -mark
    .NET Client Program Manager 
    Microsoft 
    This post is provided "as-is"

  • Booker

    I wish I could help, but I've had no luck with the previous suggestions. You'll probably find that neither the date or any non-string type filds will sort properly. There's got to be easy answer to this. I'm tempted to just find a third party datagridview-type control that's built better.
  • DataGridView Soring