如何使用外部组件对listview值进行排序?这意味着,用户不要单击列表视图的标题,而是选择服务于用户可以排序的特定标题的组合框值。我在listview中有大约6列。我只需要通过选择组合框来对这个列表视图进行排序,我将编写一个代码来根据combobox1.text值对listview进行排序。任何想法?
答案 0 :(得分:0)
您需要编写一个实现System.Collection.IComparer的类, 并根据您的下拉值设置比较规则。 那么你需要使用你编写的比较器的实例设置listview ListViewItemSorter属性,并将其命名为Sort方法。
答案 1 :(得分:0)
我终于找到了一些问题的答案。我要做的就是为我的评论中的@zohar建议为IComparer创建另一个类。请在下面找到我的答案: 我的列表视图名称是lvshop。
在课程顶部,声明:
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections
在课堂上,声明:
Private m_SortingColumn As ColumnHeader
在Windows加载时,我创建了一个列标题:
lvShop.View = View.Details
lvShop.Columns.Add("Shop No", 70, HorizontalAlignment.Left)
lvShop.Columns.Add("Shop Name", 170, HorizontalAlignment.Left)
lvShop.Columns.Add("Shop Details", 500, HorizontalAlignment.Left)
lvShop.Columns.Add("Category", 100, HorizontalAlignment.Left)
lvShop.Columns.Add("Website", 170, HorizontalAlignment.Left)
lvShop.Columns.Add("Phone No", 100, HorizontalAlignment.Left)
在listview事件
Private Sub lvShop_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvShop.ColumnClick
Dim new_sorting_column As ColumnHeader = lvShop.Columns(e.Column)
Try
' Figure out the new sorting order.
Dim sort_order As System.Windows.Forms.SortOrder
If m_SortingColumn Is Nothing Then
' New column. Sort ascending.
sort_order = SortOrder.Ascending
Else
' See if this is the same column.
If new_sorting_column.Equals(m_SortingColumn) Then
' Same column. Switch the sort order.
If m_SortingColumn.Text.StartsWith("> ") Then
sort_order = SortOrder.Descending
Else
sort_order = SortOrder.Ascending
End If
Else
' New column. Sort ascending.
sort_order = SortOrder.Ascending
End If
' Remove the old sort indicator.
m_SortingColumn.Text = _
m_SortingColumn.Text.Substring(2)
End If
' Display the new sort order.
m_SortingColumn = new_sorting_column
If sort_order = SortOrder.Ascending Then
m_SortingColumn.Text = "> " & m_SortingColumn.Text
Else
m_SortingColumn.Text = "< " & m_SortingColumn.Text
End If
' Create a comparer.
lvShop.ListViewItemSorter = New ListViewColumnSorter(e.Column, sort_order)
' Sort.
lvShop.Sort()
Catch ex As Exception
strErrMsg = "Oops! Something is wrong with sorting column header."
MessageBox.Show(strErrMsg & vbCrLf & "Err: " & ex.Message)
End Try
End Sub
在项目中创建一个名为ListViewColumnSorter
的新类
复制并粘贴到班级。
Imports System.Collections
Imports System.Windows.Forms
Public Class ListViewColumnSorter
Implements IComparer
Private m_ColumnNumber As Integer
Private m_SortOrder As SortOrder
Public Sub New(ByVal column_number As Integer, ByVal _
sort_order As SortOrder)
m_ColumnNumber = column_number
m_SortOrder = sort_order
End Sub
' Compare the items in the appropriate column
' for objects x and y.
Public Function Compare(ByVal x As Object, ByVal y As _
Object) As Integer Implements _
System.Collections.IComparer.Compare
Dim item_x As ListViewItem = DirectCast(x, _
ListViewItem)
Dim item_y As ListViewItem = DirectCast(y, _
ListViewItem)
' Get the sub-item values.
Dim string_x As String
If item_x.SubItems.Count <= m_ColumnNumber Then
string_x = ""
Else
string_x = item_x.SubItems(m_ColumnNumber).Text
End If
Dim string_y As String
If item_y.SubItems.Count <= m_ColumnNumber Then
string_y = ""
Else
string_y = item_y.SubItems(m_ColumnNumber).Text
End If
' Compare them.
If m_SortOrder = SortOrder.Ascending Then
If IsNumeric(string_x) And IsNumeric(string_y) _
Then
Return Val(string_x).CompareTo(Val(string_y))
ElseIf IsDate(string_x) And IsDate(string_y) _
Then
Return DateTime.Parse(string_x).CompareTo(DateTime.Parse(string_y))
Else
Return String.Compare(string_x, string_y)
End If
Else
If IsNumeric(string_x) And IsNumeric(string_y) _
Then
Return Val(string_y).CompareTo(Val(string_x))
ElseIf IsDate(string_x) And IsDate(string_y) _
Then
Return DateTime.Parse(string_y).CompareTo(DateTime.Parse(string_x))
Else
Return String.Compare(string_y, string_x)
End If
End If
End Function
End Class
此排序将对列表视图中的所有列进行排序。感谢互联网连接程序员:)