Solved

Listview sort order

Posted on 2010-11-30
6
264 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

706 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

14 Experts available now in Live!

Get 1:1 Help Now