Results 1 to 3 of 3

Thread: Sorting Columns in Listview

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2002
    Location
    Newcastle, Australia
    Posts
    42

    Sorting Columns in Listview

    Hi,

    I would like to add the functionality of sorting by cliking on the Headings in my ListView. Is this possible?

    Does the control take care of this for you? Do you have to code it? How?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    You can set the Sorting property and then use the Sort method to sort the first column, ascending or descending, if you want more than that you instead have to create you own comparer and set the ListViewItemSorter. If you want an example I created a Comparer to sort dates in any column...

    Code:
    Class CompareByDate
        Implements IComparer
    
        Dim index As Integer
    
        Sub New(ByVal subitemIndex As Integer)
            Me.index = subitemIndex
        End Sub
    
        Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
            Implements System.Collections.IComparer.Compare
            Dim item1 As ListViewItem = CType(x, ListViewItem)
            Dim item2 As ListViewItem = CType(y, ListViewItem)
            Return Date.Compare(Date.Parse(item1.SubItems(index).Text), Date.Parse(item2.SubItems(index).Text))
        End Function
    
    End Class
    And to use it....

    Code:
    ListView1.ListViewItemSorter = New CompareByDate(0)
    Last edited by Athley; Dec 19th, 2002 at 02:37 AM.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2002
    Location
    Newcastle, Australia
    Posts
    42

    [resolved]

    Thankyou,

    I have found a MSDN article entitled:

    Sorting ListView Items by Column Using Windows Forms

    That explains it pretty well, including code on how to sort by date.

    Thanks,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width