Solved

Sort Listview on Column Click

Posted on 2006-06-10
2
1,106 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

912 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