views:

385

answers:

2

I'm using a ListView control in a C# WinForms application. Items in the list are added to a ListViewGroup (in this case, grouping by country). The one thing that isn't working as expected is that the column sorting appears to be strange.

I've hooked into the ListViewItemSorter property of the ListView and everything sorts perfectly except when the country column is sorted in descending order (that is, Z-A). Regardless of how the list sort occurs the groups show in ascending order.

Can anyone give me a nudge in the right direction?

EDIT: FWIW, .NET 3.5 on Vista.

A: 

Check out ObjectListView -- it's much easier to work with.

Anton Gogolev
+1  A: 

Give this a try in your ColumnClick event:

    // Determine whether the column is the same as the last column clicked.
    if (e.Column != sortColumn)
    {
        // Set the sort column to the new column.
        sortColumn = e.Column;
        // Set the sort order to ascending by default.
        listView1.Sorting = SortOrder.Ascending;
    }
    else
    {
        // Determine what the last sort order was and change it.
        if (listView1.Sorting == SortOrder.Ascending)
            listView1.Sorting = SortOrder.Descending;
        else
            listView1.Sorting = SortOrder.Ascending;
    }

    // Call the sort method to manually sort.
    listView1.Sort();
    // Set the ListViewItemSorter property to a new ListViewItemComparer
    // object.
    this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column,
                                                      listView1.Sorting);
transmogrify