| In programs such as Outlook and Windows Explorer, you can sort the contents of a ListView control by clicking on the column headers. The sort order toggles between ascending and descending. To achieve this functionality, place the following code in a standard module: Public Sub SortListView(ByVal lvw As MSComctlLib.ListView, _ ByVal colHdr As MSComctlLib.ColumnHeader) '--set the sortkey to the column header's index - 1 lvw.SortKey = colHdr.Index - 1 lvw.Sorted = True '--toggle the sort order between ascending & descending lvw.SortOrder = 1 Xor lvw.SortOrder End Sub The final line of this code toggles the sort order of the listview between the enumerated listsortorder constants lvwAscending (0) and lvwDescending (1) each time the procedure is called. This provides the functionality that reverses the sort order each time the user clicks the column header. To call this procedure, use the Listview's Column_Click() event as follows: Private Sub lvwMyListView_ColumnClick(ByVal ColumnHeader As _ MSComctlLib.ColumnHeader) SortListView lvwMyListView, ColumnHeader End Sub |
Add a column sort feature to VB's ListView |
Express News India | Freelance ecommerce web development India