Problem with sorting listview in descending order

J

James Zhuo

Hi all

I have a bit of a problem sorting the columns of a
listview in descending order.

The following code has no problem sorting the listview in
ascending order. At every column click i switch the
sorting order. Using VS.NET breakpoints I can see that the
ListView SortingOrder is changed at run time to
Descending, but when the listview is painted, it still
displays the column in ascending order.

Here's the column click event handler:

Private Sub lvwUnallocatedDrivers_ColumnClick(ByVal
sender As Object, ByVal e As
System.Windows.Forms.ColumnClickEventArgs) Handles
lvwUnallocatedDrivers.ColumnClick
Select Case e.Column
Case 0
lvwUnallocatedDrivers.ListViewItemSorter =
New ListDriverIDSorter()
If lvwUnallocatedDrivers.Sorting =
SortOrder.Ascending Then
lvwUnallocatedDrivers.Sorting =
SortOrder.Descending
Else
lvwUnallocatedDrivers.Sorting =
SortOrder.Ascending
End If
Case 1
lvwUnallocatedDrivers.ListViewItemSorter =
New ListDriverNameSorter()
If lvwUnallocatedDrivers.Sorting =
SortOrder.Ascending Then
lvwUnallocatedDrivers.Sorting =
SortOrder.Descending
Else
lvwUnallocatedDrivers.Sorting =
SortOrder.Ascending
End If
Case 2
lvwUnallocatedDrivers.ListViewItemSorter =
New ListDepotSorter()
If lvwUnallocatedDrivers.Sorting =
SortOrder.Ascending Then
lvwUnallocatedDrivers.Sorting =
SortOrder.Descending
Else
lvwUnallocatedDrivers.Sorting =
SortOrder.Ascending
End If
End Select
End Sub


and here is one of the Icomparer Implementation:

Class ListDriverIDSorter
Implements IComparer

Public Function Compare(ByVal o1 As Object, ByVal o2
As Object) As Integer _
Implements System.Collections.IComparer.Compare
Dim item1, item2 As ListViewItem
item1 = CType(o1, ListViewItem)
item2 = CType(o2, ListViewItem)
If item1.Tag.DriverID > item2.Tag.DriverID Then
Return 1
ElseIf item1.Tag.DriverID < item2.Tag.DriverID Then
Return -1
Else
Return 0
End If
End Function

End Class

Any help is much appreciated.

Cheers
James
 
R

Roger

James,

From what I read in the documentation of the Sorting
property of a ListView this property only controls the
default alphabetical sorting feature of the ListView. If
you implement your own sorting using ListViewItemSorter
(as you are) then it is up to you to make the switch
between Ascending and Descending sorts. This is done by
altering the output of the compare method, toggling the
values before they are returned if you want to reverse
the normal sort order. This is generally achieved by
having a constructor for your Sorter class
(ListDriverIDSorter) which takes a sort order and is used
in the compare method. You keep track of the previous and
current columns sorted on in your app and toggle the sort
order passed to the Comparer class if you get consecutive
clicks on the same column.

The MSDN Technical article entitled "Sorting ListView
Items by Column Using Windows Forms" shows you how to do
this (and much better than my explanation too!!)

Cheers, Roger.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top