Solved

Sort Listview on Column Click

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

832 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