Solved

Sort Listview on Column Click

Posted on 2006-06-10
2
1,105 Views
Last Modified: 2013-12-25
I have a listview I want to sort via column click. Problem is, I want it based on content. I am using this right now:

    With lvResults
        .SortKey = ColumnHeader.Index - 1
        .SortOrder = IIf(.SortOrder = lvwAscending, lvwDescending, lvwAscending)
        .Sorted = True
    End With

Which works fine for text, but horribly for numbers.

How do I modify this so that it will sort numbers as numbers instead of a string?
0
Comment
Question by:DrDamnit
2 Comments
 
LVL 18

Accepted Solution

by:
Sethi earned 500 total points
ID: 16876721
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 16876730
Needs:
1 Form
1 Listview, named "lvResults" (CTRL-T / Microsoft Windows Common Controls 6.0)

======================================
Please run the program. Then click on the 1st ColumnHeader
======================================



Private Sub Form_Load()
Move 0, 0, Screen.Width, Screen.Height
 lvResults.Move 0, 0, Width, Height * 0.85
    Me.Caption = "ListView"
     lvResults.GridLines = True
     lvResults.FullRowSelect = True
   
     lvResults.View = lvwReport 'lvwReport is needed to show the list items one below the other
 
     lvResults.ColumnHeaders.Clear
     lvResults.ColumnHeaders.Add , , "Index"
     lvResults.ColumnHeaders.Add , , "Description1"
     lvResults.ColumnHeaders.Add , , "Description2"
     lvResults.ColumnHeaders.Item(1).Width = 600
     lvResults.ColumnHeaders.Item(2).Width = 9900
     lvResults.ColumnHeaders.Item(3).Width = 4500
    Dim o As ListItem
   

Dim y&
    For y = 0 To 19
        Set o = lvResults.ListItems.Add(y + 1, , CStr(r))
         lvResults.ListItems(y + 1).Text = Format(y + 1, "0#")
    Next

End Sub




Private Sub lvResults_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)

    lvResults.SortKey = ColumnHeader.Index - 1
    If lvResults.Sorted = 0 Then lvResults.Sorted = True: Exit Sub
    lvResults.SortOrder = IIf(lvResults.SortOrder, 0, 1)

End Sub

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now