Solved

Listview sort order

Posted on 2010-11-30
6
269 Views
Last Modified: 2012-05-10
I have a listview with numbers in the first column which I would like to order sequentially.  At the moment my order is

1
10
100
1000
2
20
200
3
30
300

and so on,  I need it to be

1
2
3
10
20
30
100
200
300
1000

and so on.  Im guessing this is relatively simple but how do I achieve it?
0
Comment
Question by:simonwait
6 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 34239497
ListView - Sorting by Number or Date
http://www.codeguru.com/vb/controls/vb_listview/article.php/c1669


(by the way: is is .net or classic vb6 ?)
0
 
LVL 1

Author Comment

by:simonwait
ID: 34239594
So I think that should give me the following code however not im struggling with the "With" just before the sort (with objcurrli)

Oh its classic vb6
Private Sub LoadCustomerListView()
'-----------------------------------------------------------------------------
                                 
    Dim strSQL      As String
    Dim objCurrLI   As ListItem
    Dim strZip      As String
    Dim strPhone    As String
    Dim cueno As String
    Dim lngindex As Integer
                                 
    strSQL = "SELECT * FROM Act1 ORDER BY Q"
    ' "SELECT FirstName" _
           & "     , LastName" _
           & "     , Address" _
           & "     , City" _
           & "     , State" _
           & "     , Zip" _
           & "     , PhoneNumber" _
           & "     , CustID" _
           & "  FROM Act 1 " _
           & " ORDER BY Q" _

    
    mobjCmd.CommandText = strSQL
    Set mobjRst = mobjCmd.Execute
    
    lvwCustomer.ListItems.Clear
    
    With mobjRst
                cueno = ""
        Do Until .EOF
            Set objCurrLI = lvwCustomer.ListItems.Add(, , !Qno)
            If cueno = !Qno Then
            objCurrLI.SubItems(mlngQ_No_IDX) = "" & ""
            objCurrLI.SubItems(mlngQ_Number_IDX) = "" & ""
            objCurrLI.SubItems(mlngQ_Initiator_IDX) = "" & ""

            Else
            objCurrLI.SubItems(mlngQ_No_IDX) = !Q & ""
            objCurrLI.SubItems(mlngQ_Number_IDX) = !Initiator & ""
            objCurrLI.SubItems(mlngQ_Initiator_IDX) = !Description & ""
            End If
            objCurrLI.SubItems(mlngQ_Description_IDX) = !PB & ""
            objCurrLI.SubItems(mlngQ_PB_IDX) = !Axis & ""
            objCurrLI.SubItems(mlngQ_Axis_IDX) = !Dead & ""
            objCurrLI.SubItems(mlngQ_Dead_IDX) = !Pos & ""
            objCurrLI.SubItems(mlngQ_Position_IDX) = !Delay & ""
            objCurrLI.SubItems(mlngQ_Delay_IDX) = !Accel & ""
            objCurrLI.SubItems(mlngQ_Accel_IDX) = !Decel & ""
            objCurrLI.SubItems(mlngQ_Decel_IDX) = !Speed & ""
            objCurrLI.SubItems(mlngQ_Speed_IDX) = !CopyCalcTime & ""
            objCurrLI.SubItems(mlngQ_CopyCalc_IDX) = !StartPos & ""
            objCurrLI.SubItems(mlngQ_StartPos_IDX) = !TravelDist & ""
            objCurrLI.SubItems(mlngQ_TravelDistance_IDX) = !MoveTime & ""
            objCurrLI.SubItems(mlngQ_Time_IDX) = !Moveincdelay & ""
           ' objCurrLI.SubItems(mlngQ_TimeincDelay_IDX) = !Movetime
               
'            objCurrLI.SubItems(mlngCUST_ID_IDX) = CStr(!CustID)
cueno = !Qno
            .MoveNext
        Loop
    End With
    lngindex = 0
      
            With .objCurrLI
                If (lngindex > 0) Then
                    For l = 1 To .Count
                        With .Item(l).ListSubItems(lngindex)
                            .Tag = .Text & Chr$(0) & .Tag
                            If IsNumeric(.Text) Then
                                If CDbl(.Text) >= 0 Then
                                    .Text = Format(CDbl(.Text), _
                                        strFormat)
                                Else
                                    .Text = "&" & InvNumber( _
                                        Format(0 - CDbl(.Text), _
                                        strFormat))
                                End If
                            Else
                                .Text = ""
                            End If
                        End With
                    Next l
                Else
                    For l = 1 To .Count
                        With .Item(l)
                            .Tag = .Text & Chr$(0) & .Tag
                            If IsNumeric(.Text) Then
                                If CDbl(.Text) >= 0 Then
                                    .Text = Format(CDbl(.Text), _
                                        strFormat)
                                Else
                                    .Text = "&" & InvNumber( _
                                        Format(0 - CDbl(.Text), _
                                        strFormat))
                                End If
                            Else
                                .Text = ""
                            End If
                        End With
                    Next l
                End If
            End With

        
    

End Sub

Open in new window

0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 34240884
try

With objCurrLI
not
With .objCurrLI
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 17

Expert Comment

by:Zhaolai
ID: 34242243
An alternative is to add a hidden column with the same data but formatted like "00000000", then you can sort the hidden column as normal sorting.
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 34245735
Several ways to perform it:
1) use a MSFlexGrid, that allows the selection of the type of ordering, for each column (not only: it also allows to order using only a selected range of rows)
2) "pad" the items with blank characters to the left, so that the sort weel be correct even using an alphanumeric ordering. For eample, if you know that the numbers will have a maximum of 6 digits only, put the number into a string and them convert it using:
dim k as long
k = 6 - len(myString)
if k > 0 then myString = Space(k) & myString
3) listview sorting using API.
4) Say, your column name id Id, which is numeric, and assume that your id goes maximum upto 9999 then on Id column click, you can do the following: when you are loading the list view, give Format(!Id, "0000")
5) If you are getting the data from a database, it would be easier to sort it before you loaded it into the Listview.

Source: http://forums.devx.com/showthread.php?t=153973
0
 
LVL 1

Author Closing Comment

by:simonwait
ID: 34247483
I actually used the 2nd suggestion (padding) the column which w2as sorted on was already a hidden column so not too much of a leap to use the padding method
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Specify ip address in my connection string for sas 2 60
JSON Response and request in VB6 application 11 425
VBA filters 2 61
Protecting vb6 & .Net code Obfuscation 18 125
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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

810 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