[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ListView Sorting VB6

Posted on 2004-11-05
8
Medium Priority
?
5,495 Views
Last Modified: 2013-12-25
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.
0
Comment
Question by:nichia
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 18

Accepted Solution

by:
JR2003 earned 1200 total points
ID: 12505920
Just sort by column 2 first and then by column 1.

ListView1.SortOrder = lvwAscending
ListView1.Sorted = False
ListView1.SortKey = 1
ListView1.Sorted = True
ListView1.SortKey = 0
ListView1.Sorted = True
0
 
LVL 15

Assisted Solution

by:ameba
ameba earned 800 total points
ID: 12506189
Hi nichia,
here's another way:
1) add temporary column (width=0)
        Dim ch As ColumnHeader
        Set ch = lvw1.ColumnHeaders.Add(10, "temp", "", 0)
2) add values from your two columns, combine like this: "AAAA_BBBB"
        ' for each row
             .SubItems(9) = .SubItems(7) & "_" & .SubItems(8)
3) sort listview by temporary column
        .SortOrder = lvwDescending
        .SortKey = 9
        .Sorted = True
4) remove temporary column
        lvw1.ColumnHeaders.Remove "temp"
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12512945
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12512956
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


0
 
LVL 27

Expert Comment

by:Dabas
ID: 12514913
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
0
 
LVL 3

Author Comment

by:nichia
ID: 12524949
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??
0
 
LVL 15

Expert Comment

by:ameba
ID: 12525747
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.
0
 
LVL 3

Author Comment

by:nichia
ID: 12583900
Thanks all!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month18 days, 4 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question