Posted on 2009-05-01
Last Modified: 2012-05-06
I have a listview wirth 9 column...
How to order all elemts by value in first column?
The first column contains only numbers similar:

please not in header click events but only via code.
Question by:luca2009
    1 Comment
    LVL 13

    Accepted Solution


    good morning!

    The ListView control is able to sort string data exclusively. If you want to sort on columns that hold numeric or date information, you must resort to a trick. Create a new ColumnHeader object, fill it with string data derived from the numbers or dates you want to sort on, sort on that column, and finally delete those items. Here's a reusable routine that does all this for you:

    try sumthing like this...

    Sub ListViewSortOnNonStringField(LV As ListView, ByVal ColumnIndex As _
        Integer, SortOrder As Integer, Optional IsDateValue As Boolean)
        Dim li As ListItem, number As Double, newIndex As Integer

        ' This speeds up things by a factor of 10 or more.
        LV.Visible = False
        LV.Sorted = False
        ' Create a new, hidden field.
        LV.ColumnHeaders.Add , , "dummy column", 1
        newIndex = LV.ColumnHeaders.Count - 1
        For Each li In LV.ListItems
            ' Extract a number from the field.
            If IsDateValue Then
                number = DateValue(li.ListSubItems(ColumnIndex - 1))
                number = CDbl(li.ListSubItems(ColumnIndex - 1))
            End If
            ' Add a string that can be sorted using the Sorted property.
            li.ListSubItems.Add , , Format$(number, "000000000000000.000")
        ' Sort on this hidden field.
        LV.SortKey = newIndex
        LV.SortOrder = SortOrder
        LV.Sorted = True
        ' Remove data from the hidden column.
        LV.ColumnHeaders.Remove newIndex + 1
        For Each li In LV.ListItems
            li.ListSubItems.Remove newIndex
        LV.Visible = True
    End Sub


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    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…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    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…

    759 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

    12 Experts available now in Live!

    Get 1:1 Help Now