jdy0803
Well-known member
I want to make listview to be sorted by columnheader click.
I made code like this.
This code works well in case of column item is String but not in case of Integer or Date.
How to change code to fit to following columns?
Column(0) : String
Column(1) : Integer
Column(2) : Date
I made code like this.
VB.NET:
[/COLOR][COLOR=#333333]Class ListViewItemComparer[/COLOR]
Implements IComparer
Private col As Integer
Private AscOrder As Boolean
Public Sub New()
col = 0
AscOrder = True
End Sub
Public Sub New(ByVal column As Integer, ByVal Ascending As Boolean)
col = column
AscOrder = Ascending
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
If AscOrder Then
Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
Else
Return [String].Compare(CType(y, ListViewItem).SubItems(col).Text, CType(x, ListViewItem).SubItems(col).Text)
End If
End Function
End Class
Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
' Set the ListViewItemSorter property to a new ListViewItemComparer
' object. Setting this property immediately sorts the
' ListView using the ListViewItemComparer object.
'Change this based on which direction you want
Dim blnAscending As Boolean = True
bSortToggle = Not bSortToggle
Me.ListView1.ListViewItemSorter = New ListViewItemComparer(e.Column, blnAscending) [COLOR=#333333] End Sub[/COLOR][COLOR=#333333]
How to change code to fit to following columns?
Column(0) : String
Column(1) : Integer
Column(2) : Date