2017-11-18 393 views
0

我不明白爲什麼這不起作用。我有一個ListView,我希望能夠按升序和降序排序。當逐行逐行通過代碼時看起來一切順利,除了lvItems.Sorting將不等於Descending。它在None {0}和Ascending {1}之間處於一個常量循環中。VB如何按升序和降序對ListView進行排序

這裏是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 

這裏是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 

不管是什麼數據我把我只能似乎得到升序排列。讓我知道是否有必要提供更多信息。

+1

不要設置'Sorting'和'ListViewItemSorter'。正如文檔所述,第一個用於按字母順序自動排序,第二個用於自定義排序。挑一個並使用它。 – jmcilhinney

回答

0

感謝@ jmcilhinney的評論,我能夠解決問題。我沒有使用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() 
相關問題