Link to home
Start Free TrialLog in
Avatar of nichia
nichia

asked on

ListView Sorting VB6

I am using the Listview control shipped with VB6 and would like to sort the data.

I have setup the ListView in lvwReport mode and have multiple columns.

I know I can sort by column, but is it possible to sort this control using more than one column at a time?  I would like to sort using the Text property as the primary sort, and SubItem(1) as the secondary sort.

I need the results to be like this:

"AAAA"    "BBBB"
"AAAA"    "DDDD"
"AAAA"    "ZZZZ"
"BBBB"    "DDDD"
"BBBB"    "XXXX"

Where the second column is sorted as well.

That's all.
ASKER CERTIFIED SOLUTION
Avatar of JR2003
JR2003

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi
try this
'----------------------------------------------------------
Private Sub ListView1_ColumnClick(Index As Integer, ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    ListView1(Index).Sorted = True
    ListView1(Index).SortKey = ColumnHeader.SubItemIndex
    If ListView1(Index).SortOrder = lvwAscending Then
        ListView1(Index).SortOrder = lvwDescending
    Else
        ListView1(Index).SortOrder = lvwAscending
    End If
End Sub
'--------------------------------------------------------------

;-)
Shiju
Hi

Sorry, ignore my previous post
consider this one

----------------------------------------------------------
Private Sub ListView1_ColumnClick( ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    ListView1.Sorted = True
    ListView1.SortKey = ColumnHeader.SubItemIndex
    If ListView1.SortOrder = lvwAscending Then
        ListView1.SortOrder = lvwDescending
    Else
        ListView1.SortOrder = lvwAscending
    End If
End Sub
'--------------------------------------------------------------

;-)
Shiju


Hi nichia:
ameba beat me to the solution I would propose.
The hidden column is a good way to go.
It also works if you want to sort by a date column, but present it in a normal format.
By having a hidden column, with the data formatted to "yyyymmdd", you can overcome date sorting issues...

Dabas
Avatar of nichia
nichia

ASKER

So far JR's solution works best for me, although I do not like the idea of sorting twice.

Ameba - I tried the temp column method, and it does sort properly. However, after I delete the column i get runtime errors while looping the Subitems.  I think that it thought the column data was still there, but the column header was deleted??
The column data will still be there, that's true.
If that is a problem, you can do a loop and remove it:
     For i = 1 To lvw1.Listitems.Count
         lvw1.ListItems(i).ListSubItems.Remove 9
     Next

But I suggest you change your loop which gives the error, to not include the last column.
Avatar of nichia

ASKER

Thanks all!