VB.NET中LISTVIEW控件点击列首排序问题

最近写的程序需要点击LISTVIEW控件的列首排序,搜索了一下网上的文章代码,很少,而且是C#的代码,看了下MSDN,终于搞清楚了VB的写法,MSDN上只有按照字母顺序排序的,于是我加工了一下,可以选择按照数字、时间排序。

LISTVIEW控件名是LV1

 

    Private Sub LV1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles LV1.ColumnClick

        Select Case LV1.Columns.Item(e.Column).Text
            Case "状态"
                LV1.ListViewItemSorter = New ListViewItemComparerByInt(e.Column)
            Case "编号"
                LV1.ListViewItemSorter = New ListViewItemComparerByInt(e.Column)
            Case "最后更新时间"
                LV1.ListViewItemSorter = New ListViewItemComparerByTime(e.Column)
            Case Else
                LV1.ListViewItemSorter = New ListViewItemComparerByString(e.Column)
        End Select

    End Sub

 

'按照数字排序接口

Class ListViewItemComparerByInt
        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.Compare
            Return [Decimal].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text) '数字
        End Function
    End Class

 

'按照字母顺序排序接口

Class ListViewItemComparerByString
    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.Compare
        Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text) '按文本比较
    End Function
End Class

 '按时间顺序排序

    Class ListViewItemComparerByTime
        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.Compare
            Return [Date].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text) '按时间比较
        End Function
    End Class

你可能感兴趣的:(VB.NET中LISTVIEW控件点击列首排序问题)