DataGridView multiple column sorting RRS feed

  • Question

  • Hi,

    I'm trying to sort multple colums in the DataGridView which is unbound.
    I would prefer to sort by using a DataTable/DataView but for some reason another developer in my team experienced other problems relating to sorting so he abandoned using the DataTable to fill the grid :(

    Here is the senario:
    I want the user to be able to choose which columns they want sort on. The way I envisaged was to show a form with a listbox, a grid and 3 buttons (Add to sort, Remove from sort and Apply Sort).

    The list box would be populated from the parent form grid column headings.
    The grid will show the columns and sort direction.
    Button 1 would add the selected item of the list box to the grid.
    Button 2 would remove from the grid the selected row.
    Button 3 would Apply the Sort in the order of the grid and the columns SortDirection.

    Now I looked at How to: Customize Sorting in the Windows Forms DataGridView Control using the IComparer (http://msdn.microsoft.com/en-us/library/ms171608.aspx) and I'm totally confused by the whole thing.

    If someone could explain how I can use the IComparer to sort on multiple columns that a user has specified or another way to sort on multiple columns using unbound data.

    Tuesday, June 9, 2009 12:56 PM


All replies

  • Does this helps you?


    At the very bottom is an example of a multi-column sort.

    It uses Cells[0] and Cells[1] as the way to define which columns to sort (column 0 and 1). You should be able to change this to any column you would like.

    Hope this helps.

    BTW - you are right that it would be easier to sort a DataView and bind...
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, June 10, 2009 12:27 AM
  • By the way, there is a faq tell about this:

    FAQ21: How do i sort on multiple columns?

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Wednesday, June 17, 2009 3:06 AM
  • Thank you all.

    We have now created a Multiple Sort feature to our Grid. Applogies for taking so long to respond.

    Monday, July 27, 2009 2:17 PM