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