用户登录  |  用户注册
首 页源码下载网络学院最新源码源码排行屏蔽广告
当前位置:新兴网络 > 网络学院 > .Net技术 > VB.Net

VB.NET 单击ListView表头排序的实现

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2011-08-04 13:50:35

先添加一个类。


VBScript code复制代码
Public Class ColumnSort : Implements IComparer
    Private columnNum As Integer = 0
    Public bAscending As Boolean = True
    Public Sub New(ByVal column_to_sort As Integer)
        columnNum = column_to_sort
    End Sub
    Private Function CompareInt(ByVal x As Integer, ByVal y As Integer) As Integer
        If x > y Then
            Return 1
        ElseIf x < y Then
            Return -1
        Else
            Return 0
        End If
    End Function
    Public Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare
        Dim listItemA As System.Windows.Forms.ListViewItem = DirectCast(a, System.Windows.Forms.ListViewItem)
        Dim listItemB As System.Windows.Forms.ListViewItem = DirectCast(b, System.Windows.Forms.ListViewItem)
        Dim xText As String = listItemA.SubItems(columnNum).Text
        Dim yText As String = listItemB.SubItems(columnNum).Text
        Dim xInt As Integer, yInt As Integer

        If bAscending Then
            If Integer.TryParse(xText, xInt) AndAlso Integer.TryParse(yText, yInt) Then
                Return CompareInt(xInt, yInt)
            Else
                Return [String].Compare(xText, yText)
            End If
        Else
            If Integer.TryParse(xText, xInt) AndAlso Integer.TryParse(yText, yInt) Then
                Return -1 * CompareInt(xInt, yInt)
            Else
                Return -1 * [String].Compare(xText, yText)
            End If
        End If
    End Function
End Class
ListView控件的ColumnClick事件调用示例:
VBScript code复制代码
Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
 Dim columnsort As New ColumnSort(e.Column)
 columnsort.bAscending = (ListView1.Sorting = SortOrder.Ascending)
 If columnsort.bAscending Then
     ListView1.Sorting = SortOrder.Descending
 Else
     ListView1.Sorting = SortOrder.Ascending
 End If
 ListView1.ListViewItemSorter = columnsort 
 ListView1.ListViewItemSorter = Nothing
End Sub

Tags:ListView Sorting 排序 VB.Net

作者:佚名
  • 好的评价 如果您觉得此文章好,就请您
      100%(6)
  • 差的评价 如果您觉得此文章差,就请您
      0%(0)

网络学院评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论