如何在列表视图中对其他列进行排序?我只能排序第一列

时间:2016-04-07 02:52:16

标签: vb.net sorting

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

    Select Case e.Column
        ' Nach Namen sortieren

        Case 0
            If ListView1.Columns.Item(1).ListView.Sorting <> SortOrder.Descending Then
                ListView1.Columns.Item(1).ListView.Sorting = SortOrder.Descending
            ElseIf ListView1.Columns.Item(1).ListView.Sorting <> SortOrder.Ascending Then
                ListView1.Columns.Item(1).ListView.Sorting = SortOrder.Ascending
            End If
        Case 1
            If ListView1.Columns.Item(2).ListView.Sorting <> SortOrder.Descending Then
                ListView1.Columns.Item(2).ListView.Sorting = SortOrder.Descending
            ElseIf ListView1.Columns.Item(2).ListView.Sorting <> SortOrder.Ascending Then
                ListView1.Columns.Item(2).ListView.Sorting = SortOrder.Ascending
            End If
    End Select

End Sub

这是我的代码,即使我使用数字(1),第一列仍然是正在排序的列

1 个答案:

答案 0 :(得分:0)

始终对第一列进行排序的原因是ListView1.Columns.Item(1).ListView从文档中引用了“System.Windows.Forms.ColumnHeader所在的System.Windows.Forms.ListView控件”。所以最后.ListView总是引用ListView1。

See Microsoft's answer on MSDN

从MSDN获取的代码:

排序处理程序

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    AddHandler ListView1.ColumnClick, AddressOf Me.listView1_ColumnClick
End Sub

Dim sortColumn As Integer = -1

Private Sub listView1_ColumnClick(sender As Object, e As System.Windows.Forms.ColumnClickEventArgs)
    ' Determine whether the column is the same as the last column clicked.
    If e.Column <> sortColumn Then
        ' Set the sort column to the new column.
        sortColumn = e.Column
        ' Set the sort order to ascending by default.
        listView1.Sorting = SortOrder.Ascending
    Else
        ' Determine what the last sort order was and change it.
        If listView1.Sorting = SortOrder.Ascending Then
            listView1.Sorting = SortOrder.Descending
        Else
            listView1.Sorting = SortOrder.Ascending
        End If
    End If
    ' Call the sort method to manually sort.
    listView1.Sort()
    ' Set the ListViewItemSorter property to a new ListViewItemComparer
    ' object.
    listView1.ListViewItemSorter = New ListViewItemComparer(e.Column, listView1.Sorting)
End Sub

您还需要一个项目比较器,以便ListView知道如何对项目进行排序

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
        returnVal = [String].Compare(CType(x,  _
                        ListViewItem).SubItems(col).Text, _
                        CType(y, ListViewItem).SubItems(col).Text)
        ' Determine whether the sort order is descending.
        If order = SortOrder.Descending Then
            ' Invert the value returned by String.Compare.
            returnVal *= -1
        End If

        Return returnVal
    End Function
End Class