Solved

Sort Listview on Column Click

Posted on 2006-06-10
2
1,112 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
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.
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…

705 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