在.net中,微软只实现以第一列为主的排序方式,往往很难满足用户的需求,本人正好有写到这块,现把代码整理出来。
窗体名称:frmBusroute
里面的控件主要是一个listviewup、TbStationName(文本控件)等。
vb.net2005实现listview控件任意列排序Public Class FrmBusRoute
Dim bolclick As Boolean
Private Sub FrmBusRoute_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim bolclick As Boolean
Private Sub FrmBusRoute_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'初始化listview控件
With ListViewUp
With ListViewUp
.Clear()
.GridLines = True
.Columns.Add("序号", 40, HorizontalAlignment.Center)
.Columns.Add("顺序", ListViewUp.Width - 30 - 14, HorizontalAlignment.Center)
.View = View.Details
.Scrollable = True
'.HideSelection = False
.HotTracking = True
.GridLines = True
.Columns.Add("序号", 40, HorizontalAlignment.Center)
.Columns.Add("顺序", ListViewUp.Width - 30 - 14, HorizontalAlignment.Center)
.View = View.Details
.Scrollable = True
'.HideSelection = False
.HotTracking = True
End With
End Sub
End Sub
Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click
Dim item As New ListViewItem
On Error GoTo w_err
item.Text = ListViewUp.Items.Count + 1
item.SubItems.Add(TbStationName.Text)
ListViewUp.Items.Add(item)
w_err:
Dim strErr As String
strErr = "FrmLogin.Btok_Click()" + Err.Description
Call debugPro(strErr)
End Sub
On Error GoTo w_err
item.Text = ListViewUp.Items.Count + 1
item.SubItems.Add(TbStationName.Text)
ListViewUp.Items.Add(item)
w_err:
Dim strErr As String
strErr = "FrmLogin.Btok_Click()" + Err.Description
Call debugPro(strErr)
End Sub
Private Sub ListViewUp_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListViewUp.ColumnClick
'这样排序以点击的那列为主()
If bolclick = True Then
bolclick = False
ListViewUp.ListViewItemSorter = New ListViewItemComparer(e.Column, 1)
Elsevb.net2005实现listview控件任意列排序
bolclick = True
ListViewUp.ListViewItemSorter = New ListViewItemComparer(e.Column, 0)
End If
'这样排序以第一列为主
'If ListViewUp.Sorting = SortOrder.Ascending Then
' ListViewUp.Sorting = SortOrder.Descending
'Else
' ListViewUp.Sorting = SortOrder.Ascending
'End If
End Sub
End Class
''' <summary>
''' 实现的排序类
''' </summary>
''' <remarks></remarks>
Class ListViewItemComparer
Implements IComparer
'这样排序以点击的那列为主()
If bolclick = True Then
bolclick = False
ListViewUp.ListViewItemSorter = New ListViewItemComparer(e.Column, 1)
Elsevb.net2005实现listview控件任意列排序
bolclick = True
ListViewUp.ListViewItemSorter = New ListViewItemComparer(e.Column, 0)
End If
'这样排序以第一列为主
'If ListViewUp.Sorting = SortOrder.Ascending Then
' ListViewUp.Sorting = SortOrder.Descending
'Else
' ListViewUp.Sorting = SortOrder.Ascending
'End If
End Sub
End Class
''' <summary>
''' 实现的排序类
''' </summary>
''' <remarks></remarks>
Class ListViewItemComparer
Implements IComparer
Private col As Integer
Private sort As Integer
Private sort As Integer
Public Sub New()
col = 0
sort = 0
End Sub
col = 0
sort = 0
End Sub
Public Sub New(ByVal column As Integer, ByVal v_sort As Integer)
col = column
sort = v_sort
End Sub
col = column
sort = v_sort
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements IComparer.Compare
If sort = 0 Then '0默认顺序
Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
Else
Return [String].Compare(CType(y, ListViewItem).SubItems(col).Text, CType(x, ListViewItem).SubItems(col).Text)
End If
End Functionvb.net2005实现listview控件任意列排序
End Class
Implements IComparer.Compare
If sort = 0 Then '0默认顺序
Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
Else
Return [String].Compare(CType(y, ListViewItem).SubItems(col).Text, CType(x, ListViewItem).SubItems(col).Text)
End If
End Functionvb.net2005实现listview控件任意列排序
End Class