To take it one step further (this allows you to sort strings, dates and
numbers columns appropriately) Don't think I ever finished the
enumSortType.Da teSort code completely... Not sure.
HTH,
Greg
in your form's class add:
' form level variable
Private lvwColumnSorter 1 As ListViewColumnS orter
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() call
' Create an instance of a ListView column sorter and assign it
' to the ListView control.
lvwColumnSorter 1 = New ListViewColumnS orter
Me.lvw.ListView ItemSorter = lvwColumnSorter 1
End Sub
Private Sub lvw_ColumnClick (ByVal sender As Object, ByVal e As
System.Windows. Forms.ColumnCli ckEventArgs) Handles lvw.ColumnClick
Select Case e.Column
Case TheNumericColum n -- sample custom enum for my columns...
lvwColumnSorter 1.SortType =
ListViewColumnS orter.enumSortT ype.NumericSort
Case Else
lvwColumnSorter 1.SortType =
ListViewColumnS orter.enumSortT ype.AlphaSort
End Select
' Determine if the clicked column is already the column that is
' being sorted.
If (e.Column = lvwColumnSorter 1.SortColumn) Then
' Reverse the current sort direction for this column.
If (lvwColumnSorte r1.Order = SortOrder.Ascen ding) Then
lvwColumnSorter 1.Order = SortOrder.Desce nding
Else
lvwColumnSorter 1.Order = SortOrder.Ascen ding
End If
Else
' Set the column number that is to be sorted; default to
ascending.
lvwColumnSorter 1.SortColumn = e.Column
lvwColumnSorter 1.Order = SortOrder.Ascen ding
End If
' Perform the sort with these new sort options.
Me.lvw.Sort()
End Sub
Add this helper class to project:
Imports System.Collecti ons
Imports System.Windows. Forms
Public Class ListViewColumnS orter
Implements System.Collecti ons.IComparer
Private ColumnToSort As Integer
Private OrderOfSort As SortOrder
Private ObjectCompare As CaseInsensitive Comparer
Private TypeOfSort As enumSortType
Enum enumSortType
AlphaSort
NumericSort
DateSort
End Enum
Public Sub New()
' Initialize the column to '0'.
ColumnToSort = 0
' Initialize the sort order to 'none'.
OrderOfSort = SortOrder.None
' Initialize the CaseInsensitive Comparer object.
ObjectCompare = New CaseInsensitive Comparer
' Initialize the default column type to be Alpha.
TypeOfSort = enumSortType.Al phaSort
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compa re
Dim compareResult As Integer
Dim listviewX As ListViewItem
Dim listviewY As ListViewItem
' Cast the objects to be compared to ListViewItem objects.
listviewX = CType(x, ListViewItem)
listviewY = CType(y, ListViewItem)
' Compare the two items.
Select Case TypeOfSort
Case enumSortType.Al phaSort, enumSortType.Da teSort
compareResult =
ObjectCompare.C ompare(listview X.SubItems(Colu mnToSort).Text,
listviewY.SubIt ems(ColumnToSor t).Text)
Case enumSortType.Nu mericSort
Dim intx As Integer =
CInt(listviewX. SubItems(Column ToSort).Text)
Dim inty As Integer =
CInt(listviewY. SubItems(Column ToSort).Text)
If intx = inty Then
compareResult = 0
ElseIf intx > inty Then
compareResult = 1
Else
compareResult = -1
End If
End Select
' Calculate the correct return value based on the object
' comparison.
If (OrderOfSort = SortOrder.Ascen ding) Then
' Ascending sort is selected, return typical result of
' compare operation.
Return compareResult
ElseIf (OrderOfSort = SortOrder.Desce nding) Then
' Descending sort is selected, return negative result of
' compare operation.
Return (-compareResult)
Else
' Return '0' to indicate that they are equal.
Return 0
End If
End Function
Public Property SortColumn() As Integer
Set(ByVal Value As Integer)
ColumnToSort = Value
End Set
Get
Return ColumnToSort
End Get
End Property
Public Property Order() As SortOrder
Set(ByVal Value As SortOrder)
OrderOfSort = Value
End Set
Get
Return OrderOfSort
End Get
End Property
Public Property SortType() As enumSortType
Get
Return TypeOfSort
End Get
Set(ByVal Value As enumSortType)
TypeOfSort = Value
End Set
End Property
End Class
"Glenn Wilson" <Gl*********@di scussions.micro soft.com> wrote in message
news:87******** *************** ***********@mic rosoft.com...
Class ListViewItemCom parer
Implements IComparer
Private col As Integer
Public Sub New()
col = 0
End Sub
Public Sub New(ByVal column As Integer)
col = column
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compa re Return [String].Compare(CType( x, ListViewItem).S ubItems(col).Te xt,
CType(y, ListViewItem).S ubItems(col).Te xt) End Function
End Class
Public Sub ColumnClick(ByV al sender As Object, ByVal e As
ColumnClickEven tArgs) Handles listNumberList. ColumnClick
Me.listNumberLi st.ListViewItem Sorter = New ListViewItemCom parer(e.Column)
End Sub
"billuy" wrote:
Is there a way to sort by a specified column in a listview? I'd like my
users to be able to click on the column heading and have the listview sort
by that column.
Thanks,
Billyb