多列ListView,如何对指定列进行排序?

树莓派 2003-10-18 10:59:44
多列ListView,如何对指定列(SubItem)进行排序?(写在 列表头_Click 事件中)
...全文
240 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
layershow 2003-10-18
  • 打赏
  • 举报
回复
Public Class ListViewItemComparer
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
If (String.Compare((CType(x, ListViewItem)).SubItems(col).Text, (CType(y, ListViewItem)).SubItems(col).Text) = 0) Then
Return String.Compare((CType(x, ListViewItem)).SubItems(0).Text, (CType(y, ListViewItem)).SubItems(0).Text)
Else
Return String.Compare((CType(x, ListViewItem)).SubItems(col).Text, (CType(y, ListViewItem)).SubItems(col).Text)
End If
End Function
End Class

Compare 的时候 先按照单击的列排序,然后按照第0列排序
layershow 2003-10-18
  • 打赏
  • 举报
回复
刚刚写过这东西
新建一个类,实现ICompare接口
ListViewItemComparer 就是自定义的类 参数是列名
list.ListViewItemSorter = new ListViewItemComparer(e.Column)
list.Sort()
等一下,我把代码改成VB.NET
liuguangwei 2003-10-18
  • 打赏
  • 举报
回复
类ListViewSorter:
Public Class ListViewSorter
Implements System.Collections.IComparer

Private SortIndex As Integer

Public Sub New(ByVal SortIndex As Integer)

Me.SortIndex = SortIndex

End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim x1, y1 As System.Windows.Forms.ListViewItem

x1 = x

y1 = y

Return System.Collections.CaseInsensitiveComparer.Default.Compare(x1.SubItems(SortIndex).Text, y1.SubItems(SortIndex).Text)

End Function
End Class

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

ListView1.ListViewItemSorter = New ListViewSorter(e.Column)

End Sub

16,549

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧