Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a ListView control sort using all of its columns in VB .NET
KeywordsListView, sort, all columns, VB.NET
CategoriesVB.NET, Controls
 
You can make the ListView sort by setting its Sorting property to Ascending or Descending. However, the control only sorts on its items not their sub-items. It won't even use the sub-items to brak ties when the items have the same text.

To modify the control's sorting behavior, you must create a class that implements the IComparer interface. The key to this class is the Compare function. This function takes as parameters two ListView items to compare. It should return -1 if the first should come before the second in the sort order, 0 if the items are equal, and 1 if the first should come after the second.

The ListViewAllColumnComparer class has a private variable m_SortOrder that determines whether the control sorts ascending or descending. This value is set by the class's constructor.

The Compare function loops through the sub-items held by the two ListView items and compares them one sub-item at a time to determine how the two items should be ordered.

 
' Implements a comparer for ListView columns.
' Sort on all subitems.
Class ListViewAllColumnComparer
    Implements IComparer

    Private m_SortOrder As SortOrder

    Public Sub New(ByVal sort_order As SortOrder)
        m_SortOrder = sort_order
    End Sub

    ' Compare the sub-items.
    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)
        Dim string_x As String
        Dim string_y As String

        ' Compare the sub-items.
        For i As Integer = 0 To item_x.SubItems.Count - 1
            ' See if item y has this sub-item.
            If item_y.SubItems.Count <= i Then
                ' Item y does not have this sub-item.
                ' Place it before item x.
                Return 1
            End If

            ' Compare the sub-items.
            string_x = item_x.SubItems(i).Text
            string_y = item_y.SubItems(i).Text
            If string_x <> string_y Then
                If m_SortOrder = SortOrder.Ascending Then
                    Return String.Compare(string_x, _
                        string_y)
                Else
                    Return String.Compare(string_y, _
                        string_x)
                End If
            End If
        Next i

        ' Item x has run out of sub-items.
        ' See if item y has, too.
        If item_y.SubItems.Count > item_x.SubItems.Count _
            Then
            ' Item y still has sub-items.
            ' Place x before y.
            Return -1
        Else
            ' Items x and y have the exact same
            ' items. They're equal.
            Return 0
        End If
    End Function
End Class
 
The following code shows how the program can use this class to sort the ListView data. When you click the program's optObjectAscending radio button, the program creates a new ListViewAllColumnComparer object, setting its aort order to Ascending. It sets the ListView control's ListViewItemSorter property to this object and calls the control's Sort method to make it resort its data.
 
' Sort ascending using a comparer object.
Private Sub optObjectAscending_Click(ByVal sender As _
    Object, ByVal e As System.EventArgs) Handles _
    optObjectAscending.Click
    lvwBooks.ListViewItemSorter = New _
        ListViewAllColumnComparer(SortOrder.Ascending)
    lvwBooks.Sort()
End Sub
 
This program also has radio buttons that let you sort descending using a ListViewAllColumnComparer, and to sort ascending and descending using the control's Sorting property.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated