Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to sort the listview on the basis of the clicked column

How to sort the listview on the basis of the clicked column

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tasnim
    wrote on last edited by
    #1

    Hi, How can we sort the listview on the basis of the clicked column? Thanks Tasnim

    V 1 Reply Last reply
    0
    • T Tasnim

      Hi, How can we sort the listview on the basis of the clicked column? Thanks Tasnim

      V Offline
      V Offline
      Vi2
      wrote on last edited by
      #2

      Look at SortKey Property. SortKey Property (ListView Control) Returns or sets a value that determines how the ListItem objects in a ListView control are sorted. ... Remarks The Sorted property must be set to True before the change takes place. It is common to sort a list when the column header is clicked. For this reason, the SortKey property is commonly included in the ColumnClick event to sort the list using the clicked column, as determined by the sort key, and demonstrated in the following example:

      Private Sub ListView1_ColumnClick (ByVal ColumnHeader as ColumnHeader)
      ListView1.SortKey=ColumnHeader.Index-1
      End Sub
      With best wishes, Vita

      T 1 Reply Last reply
      0
      • V Vi2

        Look at SortKey Property. SortKey Property (ListView Control) Returns or sets a value that determines how the ListItem objects in a ListView control are sorted. ... Remarks The Sorted property must be set to True before the change takes place. It is common to sort a list when the column header is clicked. For this reason, the SortKey property is commonly included in the ColumnClick event to sort the list using the clicked column, as determined by the sort key, and demonstrated in the following example:

        Private Sub ListView1_ColumnClick (ByVal ColumnHeader as ColumnHeader)
        ListView1.SortKey=ColumnHeader.Index-1
        End Sub
        With best wishes, Vita

        T Offline
        T Offline
        Tasnim
        wrote on last edited by
        #3

        But how to get it in Vb.net Thanks

        D 1 Reply Last reply
        0
        • T Tasnim

          But how to get it in Vb.net Thanks

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          You'll find what your looking for if you look in the Help Index, or search for, "ListView.ColumnClick" or "ListView.ListViewItemSorter". Basically, you have to create a custom sorting class that implements the IComparer interface. Either of those ListView help topics will provide you with an example of how to do that. RageInTheMachine9532

          T 1 Reply Last reply
          0
          • D Dave Kreskowiak

            You'll find what your looking for if you look in the Help Index, or search for, "ListView.ColumnClick" or "ListView.ListViewItemSorter". Basically, you have to create a custom sorting class that implements the IComparer interface. Either of those ListView help topics will provide you with an example of how to do that. RageInTheMachine9532

            T Offline
            T Offline
            Tasnim
            wrote on last edited by
            #5

            Ya I read the help but I didnt find any examples related to it. I just came to know that it works with IComparer Interface , but how I didnt get it. Can anyone help me for it? Thanks Tasnim

            D 1 Reply Last reply
            0
            • T Tasnim

              Ya I read the help but I didnt find any examples related to it. I just came to know that it works with IComparer Interface , but how I didnt get it. Can anyone help me for it? Thanks Tasnim

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              OK. The second class in the example just implements a Sorter. What your doing is writing your own custom sorter that can sort items on any column. The only argument you need in the column number. This would allow your to write sorters for different types of data in each column. When you 'Implement' an interface, in this case IComparer, your given a framework that .NET expects you to follow. All you have to do is hang code on the methods that you have to implement. In the example in the help files, the only real code is the Compare function. .NET expects the function to take 2 parameters, both generic Objects and expects you to compare them somehow and return an Integer as a result of the compare. Looking at the documentation for IComparer.Compare (the function you have to supply code for!), the function is expected to return the following: Less than zero if x is less than y. Zero if x equals y. Greater than zero if x is greater than y. In the sample below, .NET will call you with two ListViewItem objects, x and y. Since you know that your going to be getting two ListViewItems to compare, you have to cast the two Objects as ListViewItems. Then you can use the ListViewItems.SubItems property and the column number that is passed in when this class is created (Public Sub New(ByVal column as Integer)) to get the Text to compare. Then all you have to do is use the String.Compare method on the two SubItem.Text Strings and return the value that .NET and the IComparer interface expects you to return. Check out the docs on String.Compare and you'll see that its return values match those expected by IComparer.Compare.

              ' Implements the manual sorting of items by columns.
              Class ListViewItemComparer
              Implements IComparer

                Private col As Integer
              
                Public Sub New()
                   columnNumber = 0
                End Sub
              
                Public Sub New(ByVal column As Integer)
                   columnNumber = column
                End Sub
              
                Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
                   Dim returnValue as Integer
                   Dim compX as ListViewItem = CType(x, ListViewItem)
                   Dim compY as ListViewItem = CType(y, ListViewItem)
              
                   returnValue = String.Compare(compX.SubItems(columnNumber).Text, compY.SubItems(columnNumber).Text)
                   Return returnValue
                End Function
              

              End Class

              Now that you have a comparer class written, all you have to do is tell the ListView to use it when a column header i

              T 1 Reply Last reply
              0
              • D Dave Kreskowiak

                OK. The second class in the example just implements a Sorter. What your doing is writing your own custom sorter that can sort items on any column. The only argument you need in the column number. This would allow your to write sorters for different types of data in each column. When you 'Implement' an interface, in this case IComparer, your given a framework that .NET expects you to follow. All you have to do is hang code on the methods that you have to implement. In the example in the help files, the only real code is the Compare function. .NET expects the function to take 2 parameters, both generic Objects and expects you to compare them somehow and return an Integer as a result of the compare. Looking at the documentation for IComparer.Compare (the function you have to supply code for!), the function is expected to return the following: Less than zero if x is less than y. Zero if x equals y. Greater than zero if x is greater than y. In the sample below, .NET will call you with two ListViewItem objects, x and y. Since you know that your going to be getting two ListViewItems to compare, you have to cast the two Objects as ListViewItems. Then you can use the ListViewItems.SubItems property and the column number that is passed in when this class is created (Public Sub New(ByVal column as Integer)) to get the Text to compare. Then all you have to do is use the String.Compare method on the two SubItem.Text Strings and return the value that .NET and the IComparer interface expects you to return. Check out the docs on String.Compare and you'll see that its return values match those expected by IComparer.Compare.

                ' Implements the manual sorting of items by columns.
                Class ListViewItemComparer
                Implements IComparer

                  Private col As Integer
                
                  Public Sub New()
                     columnNumber = 0
                  End Sub
                
                  Public Sub New(ByVal column As Integer)
                     columnNumber = column
                  End Sub
                
                  Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
                     Dim returnValue as Integer
                     Dim compX as ListViewItem = CType(x, ListViewItem)
                     Dim compY as ListViewItem = CType(y, ListViewItem)
                
                     returnValue = String.Compare(compX.SubItems(columnNumber).Text, compY.SubItems(columnNumber).Text)
                     Return returnValue
                  End Function
                

                End Class

                Now that you have a comparer class written, all you have to do is tell the ListView to use it when a column header i

                T Offline
                T Offline
                Tasnim
                wrote on last edited by
                #7

                Hi, Thanks a lot, Will try the code and get back to you. Thanks Tasnim.

                T 1 Reply Last reply
                0
                • T Tasnim

                  Hi, Thanks a lot, Will try the code and get back to you. Thanks Tasnim.

                  T Offline
                  T Offline
                  Tasnim
                  wrote on last edited by
                  #8

                  Also I want to sort it into descending order if the column is clicked again. How to do it? Thx Tasnim

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups