Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27201

Sort by Columnheader click

$
0
0
I want to make listview to be sorted by columnheader click.
I made code like this.
Code:

    Class ListViewItemComparer
        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)
    End Sub

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

Viewing all articles
Browse latest Browse all 27201

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>