我正在写一个家庭作业计划器,我有一个ListView元素。
我正在寻找一个代码段,当我点击列标题时,我可以按字母顺序排列列。如果它已经按升序排序,则切换到降序,反之亦然,然后按默认/第一个排序,即" status"在下面的屏幕截图中。
我用谷歌搜索过,找不到任何可以使用的东西,你能帮助我吗?
Private Sub ColumnClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.ColumnClickEventArgs) _
Handles lvKalender.ColumnClick
Select Case e.Column
' Nach Namen sortieren
Case 0
If lvKalender.Columns.Item(0).ListView.Sorting <> SortOrder.Descending Then
lvKalender.Columns.Item(0).ListView.Sorting = SortOrder.Descending
ElseIf lvKalender.Columns.Item(0).ListView.Sorting <> SortOrder.Ascending Then
lvKalender.Columns.Item(0).ListView.Sorting = SortOrder.Ascending
End If
Case 1
If (1).ListView.Sorting <> SortOrder.Descending Then
lvKalender.Columns.Item(1).ListView.Sorting = SortOrder.Descending
ElseIf lvKalender.Columns.Item(1).ListView.Sorting <> SortOrder.Ascending Then
lvKalender.Columns.Item(1).ListView.Sorting = SortOrder.Ascending
End If
Case 2
If lvKalender.Columns.Item(2).ListView.Sorting <> SortOrder.Descending Then
lvKalender.Columns.Item(2).ListView.Sorting = SortOrder.Descending
ElseIf lvKalender.Columns.Item(2).ListView.Sorting <> SortOrder.Ascending Then
lvKalender.Columns.Item(2).ListView.Sorting = SortOrder.Ascending
End If
Case 3
If lvKalender.Columns.Item(3).ListView.Sorting <> SortOrder.Descending Then
lvKalender.Columns.Item(3).ListView.Sorting = SortOrder.Descending
ElseIf lvKalender.Columns.Item(3).ListView.Sorting <> SortOrder.Ascending Then
lvKalender.Columns.Item(3).ListView.Sorting = SortOrder.Ascending
End If
Case 4
If lvKalender.Columns.Item(4).ListView.Sorting <> SortOrder.Descending Then
lvKalender.Columns.Item(4).ListView.Sorting = SortOrder.Descending
ElseIf lvKalender.Columns.Item(4).ListView.Sorting <> SortOrder.Ascending Then
lvKalender.Columns.Item(4).ListView.Sorting = SortOrder.Ascending
End If
Case 5
If lvKalender.Columns.Item(5).ListView.Sorting <> SortOrder.Descending Then
lvKalender.Columns.Item(5).ListView.Sorting = SortOrder.Descending
ElseIf lvKalender.Columns.Item(5).ListView.Sorting <> SortOrder.Ascending Then
lvKalender.Columns.Item(5).ListView.Sorting = SortOrder.Ascending
End If
Case 6
If lvKalender.Columns.Item(6).ListView.Sorting <> SortOrder.Descending Then
lvKalender.Columns.Item(6).ListView.Sorting = SortOrder.Descending
ElseIf lvKalender.Columns.Item(6).ListView.Sorting <> SortOrder.Ascending Then
lvKalender.Columns.Item(6).ListView.Sorting = SortOrder.Ascending
End If
End Select
End Sub
答案 0 :(得分:3)
如果我理解你的问题,你想在ListView
中对一列日期(可能还有一些值???)进行排序。问题是ListView
中的所有内容都是字符串/文本。一些子项目(cols)可能看起来像一样,但这只是因为这是你的大脑翻译/解释它们的方式 - 它们实际上就是字符串。
这样做的影响是排序有数字的文本很少有用。例如,"90"
的排序总是高于"300"
,因为"9"
&gt; "3"
和"19.08.2014"
排序高于"10.01.2019"
。 数字和数字的值不相同。
要按文字以外的其他内容排序,您需要ListViewItemSorter
。这个是日期:
Friend Class ListViewItemDateComparer
Implements IComparer
Private col As Integer
Private _sort As SortOrder = SortOrder.Ascending
Public Sub New(column As Integer, sort As Windows.Forms.SortOrder)
col = column
_sort = sort
End Sub
Public Function Compare(x As Object,
y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim returnVal As Integer = -1
' parse LV contents back to DateTime value
Dim dtX As DateTime = DateTime.Parse(CType(x, ListViewItem).SubItems(col).Text)
Dim dtY As DateTime = DateTime.Parse(CType(y, ListViewItem).SubItems(col).Text)
' compare
returnVal = DateTime.Compare(dtX, dtY)
If _sort = SortOrder.Descending Then
returnVal *= -1
End If
Return returnVal
End Function
End Class
使用它:
Dim LVSorter = New ListViewItemDateComparer(COL_TO_SORT, Sort_Order)
myLV.ListViewItemSorter = LVSorter
COL_TO_SORT
将是您要排序的包含日期的列/子项索引。
Sort_Order
将是标准的WinForms SortOrder(SortOrder.Ascending
等)
比较其他数据类型,例如Decimal
,克隆Comparer
并更改进行比较的3行:
' parse LV contents back to Decimal value
Dim dtX As Decimal = Decimal.Parse(CType(x, ListViewItem).SubItems(col).Text)
Dim dtY As Decimal = Decimal.Parse(CType(y, ListViewItem).SubItems(col).Text)
' compare
returnVal = Decimal.Compare(dtX, dtY)
答案 1 :(得分:0)
Private Sub ColumnClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.ColumnClickEventArgs) _
Handles lvKalender.ColumnClick
If lvKalender.Columns.Item(e.Column).ListView.Sorting <> SortOrder.Descending Then
lvKalender.Columns.Item(e.Column).ListView.Sorting = SortOrder.Descending
ElseIf lvKalender.Columns.Item(e.Column).ListView.Sorting <> SortOrder.Ascending Then
lvKalender.Columns.Item(e.Column).ListView.Sorting = SortOrder.Ascending
End If
End Sub
答案 2 :(得分:0)
Private Sub ListViewProcess_ColumnClick(sender As Object, e As ColumnClickEventArgs) Handles ListViewProcess.ColumnClick
sender.AutoResizeColumn(e.Column, ColumnHeaderAutoResizeStyle.HeaderSize)
If sender.Columns.Item(e.Column).ListView.Sorting <> SortOrder.Descending Then
sender.Columns.Item(e.Column).ListView.Sorting = SortOrder.Descending
ElseIf sender.Columns.Item(e.Column).ListView.Sorting <> SortOrder.Ascending Then
sender.Columns.Item(e.Column).ListView.Sorting = SortOrder.Ascending
End If
End Sub