如何解决vb.net对listview进行排序,然后为备用行着色
我可以使列表视图交替显示彩色行或进行排序,但不能同时显示两者。在替换行被着色导致行的颜色混乱之前,排序似乎并不完整,但是在列表视图中进行了排序。如何在调用函数SetAlternateColors之前确保listviewItemSorter已完成?
lvGedcom.ListViewItemSorter = New ListViewComparer(0,SortOrder.Ascending)
lvGedcom.Sort()
InfraT.SetAlternateColor(lvGedcom)
排序后我尝试睡觉...
Public Shared Sub SetAlternateColor(sender As Object)
'Set color striping on listview
For i As Integer = 0 To sender.Items.Count - 1 Step 2
sender.Items(i).BackColor = Color.LightGoldenrodYellow
If i < sender.items.Count - 1 Then
sender.Items(i + 1).BackColor = SystemColors.Window
End If
Next
End Sub
来自http://www.vb-helper.com/howto_net_listview_sort_clicked_column.html
的自定义分类器Class ListViewComparer
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
任何帮助都会很棒!
解决方法
用于填充和排序所需的listview的调用来自于load事件,而不是来自new事件,然后它就像一个超级按钮一样工作。感谢Jimi的协助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。