1

如何在不使用 listview1_columnclick 的情况下使用 vb.net 2008 对 listview 中的数据进行排序?

这是我的代码,它没有正确排序。

       If ListView1.Sorting = SortOrder.Ascending Then
        ListView1.Sorting = SortOrder.Descending
       Else
        ListView1.Sorting = SortOrder.Ascending
       End If

非常感谢您的回复。

-简

4

2 回答 2

1

您需要告诉 ListView 如何进行排序。

在 C# 中:

创建一个比较器:

// Implements the manual sorting of items by columns. 
class ListViewItemComparer : IComparer
{
    private int col;
    public ListViewItemComparer()
    {
        col = 0;
    }
    public ListViewItemComparer(int column)
    {
        col = column;
    }
    public int Compare(object x, object y)
    {
        return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
    }
}

然后通过调用调用排序:

this.listView1.ListViewItemSorter = new ListViewItemComparer(colNum);

其中colNum是要排序的列的索引。

示例取自这里:http: //msdn.microsoft.com/en-us/library/system.windows.forms.listview.columnclick.aspx

这是 VB.net 中的解决方案:

创建一个比较器:

' Implements the manual sorting of items by columns. 
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
        Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
    End Function 
End Class

然后通过调用调用排序:

Me.listView1.ListViewItemSorter = New ListViewItemComparer(colNum)

其中colNum是要排序的列的索引。

于 2013-08-08T09:40:32.550 回答
1

如果要切换升序/降序,则还需要跟踪“排序顺序”。在您的比较器和您的列中单击事件处理程序。

例子:

ListViewItemComparer.vb:

Public Class ListViewItemComparer
    ...
    Private col As Integer
    Private order As SortOrder
    ...
    Public Sub New()
        col = 0
        order = SortOrder.Ascending
    ...
    Public Sub New(ByVal column As Integer, order As SortOrder)
       ...
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer
        ...
        ' Perform "compare": -1: s1 < s2; 0: s1=s2, 1: s1 > s2
        Dim retval As Integer = -1
        retval = [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)

        ' Reverse if "descending"
        If order = SortOrder.Descending Then retval *= -1

Form1.vb

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      ...
      ' Allow "reverse" sort
      ' Initial order is "NONE".  As any column is clicked, the order will toggle between Ascending (first), then Descending
      ReDim ColumnOrder(4)
      For i As Integer = 0 To ColumnOrder.Length - 1
          ColumnOrder(i) = SortOrder.Descending
      Next
   ...
   Private Sub ListView1_ColumnClick(sender As Object, e As ColumnClickEventArgs) Handles ListView1.ColumnClick
      Dim ColumnHeader As System.Windows.Forms.ColumnHeader = ListView1.Columns(e.Column)
      If ColumnOrder(e.Column) = SortOrder.Descending Then
          ColumnOrder(e.Column) = SortOrder.Ascending
      Else
          ColumnOrder(e.Column) = SortOrder.Descending
      End If
      ListView1.Sorting = ColumnOrder(e.Column)
      ListView1.Sort()
      ListView1.ListViewItemSorter = New ListViewItemComparer(e.Column, ListView1.Sorting)
      ...

在此处查看更多详细信息:MSDN:使用 Windows 窗体按列对 ListView 项进行排序

于 2015-08-14T19:34:54.153 回答