VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Sorting by columnheaders in listviews

by Adam Hansen (1 Submission)
Category: Miscellaneous
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Date Added: Wed 3rd February 2021
Rating: (39 Votes)

This allows a user to sort the contents of a listview by clicking on a column header in report-mode. Clicking again will sort descending.

Assumes
This assumes that a ListView named 'ListView1' exists on a form, and that code exists to put values into it. Also, the ListView style-property must be 'report'. Paste the code into the 'General Declarations' area of your form (below any other global declarations you may have) and substitute 'ListView1' with the name of your listview.

Rate Sorting by columnheaders in listviews

Dim iCol As Integer
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As_ MSComctlLib.ColumnHeader)
  
  ' When a ColumnHeader object is clicked, the ListView control is
  ' sorted by the subitems of that column.
  ' Set the SortKey to the Index of the ColumnHeader - 1
  
  If ColumnHeader.Index - 1 <> iCol Then
    ListView1.SortOrder = 0
  Else
    ListView1.SortOrder = Abs(ListView1.SortOrder - 1)
  End If
  
  ListView1.SortKey = ColumnHeader.Index - 1
  
  ' Set Sorted to True to sort the list.
  
  ListView1.Sorted = True
  iCol = ColumnHeader.Index - 1
End Sub

Download this snippet    Add to My Saved Code

Sorting by columnheaders in listviews Comments

No comments have been posted about Sorting by columnheaders in listviews. Why not be the first to post a comment about Sorting by columnheaders in listviews.

Post your comment

Subject:
Message:
0/1000 characters