简体   繁体   English

VB如何对ListView进行升序和降序排序

[英]VB How to sort ListView in ascending and descending order

I cannot figure out why this is not working. 我不知道为什么这不起作用。 I have a ListView that I would like to be able to sort in both ascending and descending order. 我有一个ListView ,我希望能够以升序和降序排序。 It appears when stepping through the code line by line that all goes well except lvItems.Sorting will not equal Descending . 一行一行地执行代码时,除了lvItems.Sorting并不等于Descending之外,其他所有代码都运行良好。 It is in a constant loop between None{0} and Ascending{1}. 它处于None {0}和Ascending {1}之间的恒定循环中。

Here is the Form class: 这是Form类:

Private Sub lvItems_ColumnClick(sender As Object, e As System.Windows.Forms.ColumnClickEventArgs) Handles lvItems.ColumnClick

    ' If current column is not the previously clicked column
    ' Add
    If e.Column <> sortColumn Then

        ' Set the sort column to the new column
        sortColumn = e.Column

        'Default to ascending sort order
        lvItems.Sorting = SortOrder.Ascending

    Else

        'Flip the sort order
        If lvItems.Sorting = SortOrder.Ascending Then
            lvItems.Sorting = SortOrder.Descending
        Else
            lvItems.Sorting = SortOrder.Ascending
        End If
    End If

    'Set the ListviewItemSorter property to a new ListviewItemComparer object
    Me.lvItems.ListViewItemSorter = New ListViewItemComparer(e.Column, lvItems.Sorting)

    ' Call the sort method to manually sort
    lvItems.Sort()

End Sub

And here is the ListViewItemComparer Class: 这是ListViewItemComparer类:

Public Class ListViewItemComparer

    Implements IComparer

    Private col As Integer
    Private order As SortOrder

    Public Sub New()
        col = 0
        order = SortOrder.Ascending
    End Sub

    Public Sub New(column As Integer, order As SortOrder)
        col = column
        Me.order = order
    End Sub

    Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare

        Dim returnVal As Integer = -1

        Try

            ' Attempt to parse the two objects as DateTime
            Dim firstDate As System.DateTime = DateTime.Parse(CType(x, ListViewItem).SubItems(col).Text)
            Dim secondDate As System.DateTime = DateTime.Parse(CType(y, ListViewItem).SubItems(col).Text)

            ' Compare as date
            returnVal = DateTime.Compare(firstDate, secondDate)

        Catch ex As Exception

            ' If date parse failed then fall here to determine if objects are numeric
            If IsNumeric(CType(x, ListViewItem).SubItems(col).Text) And
                IsNumeric(CType(y, ListViewItem).SubItems(col).Text) Then

                ' Compare as numeric
                returnVal = Val(CType(x, ListViewItem).SubItems(col).Text).CompareTo(Val(CType(y, ListViewItem).SubItems(col).Text))

            Else
                ' If not numeric then compare as string
                returnVal = [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
            End If

        End Try

        ' If order is descending then invert value
        If order = SortOrder.Descending Then
            returnVal *= -1
        End If

        Return returnVal

    End Function

End Class

No matter what data I put in I only seem to get ascending order. 无论我输入什么数据,我似乎都只会升序。 Let me know if more information is necessary. 让我知道是否需要更多信息。

I was able to resolve issue thanks to @jmcilhinney's comment. 感谢@jmcilhinney的评论,我得以解决问题。 Instead of using both Sorting and ListViewItemSorter , I created a string variable ordering and assigned it the appropriate sort order (see below for final solution). 我没有同时使用SortingListViewItemSorter ,而是创建了一个字符串变量ordering并为其分配了适当的排序顺序(有关最终解决方案,请参见下文)。

        'If current column is not the previously clicked column
        'Add
        If e.Column <> sortColumn Then

            ' Set the sort column to the new column
            sortColumn = e.Column

            'Default to ascending sort order
            ordering = "Ascending"

        Else

            'Flip the sort order
            If ordering = "Ascending" Then
                ordering = "Descending"
            Else
                ordering = "Ascending"
            End If
        End If

        'Set the ListviewItemSorter property to a new ListviewItemComparer object
        lvItems.ListViewItemSorter = New ListViewItemComparer(e.Column, ordering)

        'Call the sort method to manually sort
        lvItems.Sort()

If you have created a string variable ordering, you need change the ListViewItemComparer class. 如果创建了字符串变量排序,则需要更改ListViewItemComparer类。

Public Class ListViewItemComparer 公共类ListViewItemComparer

Implements IComparer

Private col As Integer
Private order As String

Public Sub New()
    col = 0
    order = "Ascending"
End Sub

Public Sub New(column As Integer, order As String)
    col = column
    Me.order = order
End Sub

Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare

    Dim returnVal As Integer = -1

    Try

        ' Attempt to parse the two objects as DateTime
        Dim firstDate As System.DateTime = DateTime.Parse(CType(x, ListViewItem).SubItems(col).Text)
        Dim secondDate As System.DateTime = DateTime.Parse(CType(y, ListViewItem).SubItems(col).Text)

        ' Compare as date
        returnVal = DateTime.Compare(firstDate, secondDate)

    Catch ex As Exception

        ' If date parse failed then fall here to determine if objects are numeric
        If IsNumeric(CType(x, ListViewItem).SubItems(col).Text) And
            IsNumeric(CType(y, ListViewItem).SubItems(col).Text) Then

            ' Compare as numeric
            returnVal = Val(CType(x, ListViewItem).SubItems(col).Text).CompareTo(Val(CType(y, ListViewItem).SubItems(col).Text))

        Else
            ' If not numeric then compare as string
            returnVal = [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
        End If

    End Try

    ' If order is descending then invert value
    If order = "Descending" Then
        returnVal *= -1
    End If

    Return returnVal

End Function

End Class 最终班

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM