borderAndreaVB free resources for Visual Basic developersborder

AndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2012 Andrea Tincani
:: How to get a listview control to sort numbers correctly...

Author  

Tom Boward

Language  

VB5, VB6

Operating Systems  

Windows 95, 98 and NT
Tip

This is one I discovered after banging my head against the wall for a few hours trying to get a list view control to sort numbers correctly. When trying to create a list view control in report mode that would allow a user to quickly sort by a text listing (in this case a column of state abbreviations) or sort by counts for each state, I ran into a data type issue that is not addressed with this control. The problem is that when a numeric column is entered as the sort key, the control treats it as text. To overcome this problem, I created a third column and set its width to zero so it would not be visible at run time. I then assigned the count with several leading zero's to this column.

i.e. xitem.SubItems(3) = Right("00000000" & count, 8)
created
00000001 from 1
00000057 from 57
00331562 from 331562

The leading zero's on these numbers will cause them to sort correctly as text. When the user clicked on the visible column, I just switched the column index to the hidden one.


Private Sub lvwDisp_ColumnClick(ByVal ColumnHeader As ComctlLib.ColumnHeader)
    lvwDisp.SortKey = ColumnHeader.Index - 1
    If lvwDisp.SortKey = 1 Then lvwDisp.SortKey = 3
' **** This column changes the key
    lvwDisp.SortOrder = (lvwDisp.SortOrder - 1) * -1
    lvwDisp.Sorted = True
End Sub

:: Navigation

Home

Miscellaneous Tips

Previous Tip

Next Tip

:: Search this site
Google
:: Related Topics
icon 19-02-2006 Control Required by niyati
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2012 Andrea Tincaniborder