Results 1 to 24 of 24

Thread: [RESOLVED] Is it possible to sort a listview?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Resolved [RESOLVED] Is it possible to sort a listview?

    I've got a two-column listview that lists teams alphabetically and displays the number of cases associated with them. What if I wanted to sort the teams in descending order, or sort the list by the number of cases? Is this even possible with a listview?

  2. #2

  3. #3
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Is it possible to sort a listview?

    I do this:

    In a module:

    VB Code:
    1. Public Sub SortListView(lvwList As MSComctlLib.ListView, ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    2.         If lvwList.SortKey = (ColumnHeader.Index - 1) Then
    3.             If lvwList.SortOrder = lvwAscending Then
    4.                 lvwList.SortOrder = lvwDescending
    5.             Else
    6.                 lvwList.SortOrder = lvwAscending
    7.             End If
    8.             lvwList.Sorted = True
    9.         Else
    10.             lvwList.SortKey = (ColumnHeader.Index - 1)
    11.             lvwList.Sorted = True
    12.             lvwList.SortOrder = lvwAscending
    13.         End If
    14. End Sub

    In the form:

    VB Code:
    1. Private Sub lvwBills_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    2.     SortListView lvwBills, ColumnHeader
    3. End Sub

    It has the alpha search limitation that gavio's post looks like it takes care of.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Is it possible to sort a listview?

    Quote Originally Posted by gavio
    Here's a very good sorting sample
    I'm having a problem with that one...I implemented the code to the best of my knowledge but when I run it I'm getting this error:

    Run-time error '35613':

    ImageList must be initialized before it can be used

    So I right-click my listview, go to properties, go to the imagelists tab and there are no image lists there. I don't have columns or anything in there either since they're added programmatically when the form loads. Is there a way to initialize the image list that way? I don't really know this object very well and what I've looked up on the internet has not been helpful.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Is it possible to sort a listview?

    Quote Originally Posted by TysonLPrice
    I do this:

    In a module:

    VB Code:
    1. Public Sub SortListView(lvwList As MSComctlLib.ListView, ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    2.         If lvwList.SortKey = (ColumnHeader.Index - 1) Then
    3.             If lvwList.SortOrder = lvwAscending Then
    4.                 lvwList.SortOrder = lvwDescending
    5.             Else
    6.                 lvwList.SortOrder = lvwAscending
    7.             End If
    8.             lvwList.Sorted = True
    9.         Else
    10.             lvwList.SortKey = (ColumnHeader.Index - 1)
    11.             lvwList.Sorted = True
    12.             lvwList.SortOrder = lvwAscending
    13.         End If
    14. End Sub

    In the form:

    VB Code:
    1. Private Sub lvwBills_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    2.     SortListView lvwBills, ColumnHeader
    3. End Sub

    It has the alpha search limitation that gavio's post looks like it takes care of.
    This one sorts numbers as if they're letters, i.e. 1, 10, 11, 2, 25, 3, etc.

  6. #6
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Is it possible to sort a listview?

    Yep...

    That's what I meant when I said:

    "It has the alpha search limitation that gavio's post looks like it takes care of."

    You can try padding the numbers like this for money:

    iItem.SubItems(12) = Format$(Format$(MedBillsAdded, "Currency"), "@@@@@@@@@@@")

    And tweak it for numbers. Negative numbers become an issue because of the negative sign. For the long term and future projects look at Gavoi's post or Google for APIs that handle all that. Search this forum also. Some people assign numbers the user can't see and tweak the sort to use them.

  7. #7
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Is it possible to sort a listview?

    The trick to solving that problem is to create a hidden column with numeric formated values and sort on that column.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Is it possible to sort a listview?

    Quote Originally Posted by TysonLPrice
    Yep...

    That's what I meant when I said:

    "It has the alpha search limitation that gavio's post looks like it takes care of."

    You can try padding the numbers like this for money:

    iItem.SubItems(12) = Format$(Format$(MedBillsAdded, "Currency"), "@@@@@@@@@@@")

    And tweak it for numbers. Negative numbers become an issue because of the negative sign. For the long term and future projects look at Gavoi's post or Google for APIs that handle all that. Search this forum also. Some people assign numbers the user can't see and tweak the sort to use them.
    I tried implementing the code that gavio referred to but I keep having problems with the imagelist; apparently I'm not initializing it but I don't know how to do it programatically. I can't do it in the properties of the object because I don't add columns to it until the form loads.

  9. #9
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Is it possible to sort a listview?

    Quote Originally Posted by disruptivehair
    I tried implementing the code that gavio referred to but I keep having problems with the imagelist; apparently I'm not initializing it but I don't know how to do it programatically. I can't do it in the properties of the object because I don't add columns to it until the form loads.

    This works for me. Its the formatting when you add items.

    VB Code:
    1. Option Explicit
    2. Private iItem As ListItem
    3. Private Sub Form_Load()
    4.    
    5.     Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
    6.     iItem.SubItems(1) = Format$("1", "@@@@@@@@@@@")
    7.     Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
    8.     iItem.SubItems(1) = Format$("11", "@@@@@@@@@@@")
    9.     Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
    10.     iItem.SubItems(1) = Format$("155", "@@@@@@@@@@@")
    11.     Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
    12.     iItem.SubItems(1) = Format$("2", "@@@@@@@@@@@")
    13.     Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
    14.     iItem.SubItems(1) = Format$("6", "@@@@@@@@@@@")
    15.  
    16.  
    17. End Sub
    18. Public Sub SortListView(lvwList As MSComctlLib.ListView, ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    19.         If lvwList.SortKey = (ColumnHeader.Index - 1) Then
    20.             If lvwList.SortOrder = lvwAscending Then
    21.                 lvwList.SortOrder = lvwDescending
    22.             Else
    23.                 lvwList.SortOrder = lvwAscending
    24.             End If
    25.             lvwList.Sorted = True
    26.         Else
    27.             lvwList.SortKey = (ColumnHeader.Index - 1)
    28.             lvwList.Sorted = True
    29.             lvwList.SortOrder = lvwAscending
    30.         End If
    31. End Sub
    32. Private Sub lvwEMailEvents_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    33. On Error GoTo ErrorHandler
    34.     SortListView lvwEmailEvents, ColumnHeader
    35.     Exit Sub
    36. ErrorHandler:
    37.     Screen.MousePointer = vbNormal
    38.     MsgBox "Error lvwEMailEvents_ColumnClick at Line # " & Erl & " (" & Err.Description & ")", , "Error: lvwEMailEvents_ColumnClick"
    39. End Sub

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Is it possible to sort a listview?

    Quote Originally Posted by TysonLPrice
    This works for me. Its the formatting when you add items.

    VB Code:
    1. Option Explicit
    2. Private iItem As ListItem
    3. Private Sub Form_Load()
    4.    
    5.     Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
    6.     iItem.SubItems(1) = Format$("1", "@@@@@@@@@@@")
    7.     Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
    8.     iItem.SubItems(1) = Format$("11", "@@@@@@@@@@@")
    9.     Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
    10.     iItem.SubItems(1) = Format$("155", "@@@@@@@@@@@")
    11.     Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
    12.     iItem.SubItems(1) = Format$("2", "@@@@@@@@@@@")
    13.     Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
    14.     iItem.SubItems(1) = Format$("6", "@@@@@@@@@@@")
    15.  
    16.  
    17. End Sub
    18. Public Sub SortListView(lvwList As MSComctlLib.ListView, ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    19.         If lvwList.SortKey = (ColumnHeader.Index - 1) Then
    20.             If lvwList.SortOrder = lvwAscending Then
    21.                 lvwList.SortOrder = lvwDescending
    22.             Else
    23.                 lvwList.SortOrder = lvwAscending
    24.             End If
    25.             lvwList.Sorted = True
    26.         Else
    27.             lvwList.SortKey = (ColumnHeader.Index - 1)
    28.             lvwList.Sorted = True
    29.             lvwList.SortOrder = lvwAscending
    30.         End If
    31. End Sub
    32. Private Sub lvwEMailEvents_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    33. On Error GoTo ErrorHandler
    34.     SortListView lvwEmailEvents, ColumnHeader
    35.     Exit Sub
    36. ErrorHandler:
    37.     Screen.MousePointer = vbNormal
    38.     MsgBox "Error lvwEMailEvents_ColumnClick at Line # " & Erl & " (" & Err.Description & ")", , "Error: lvwEMailEvents_ColumnClick"
    39. End Sub
    That's the code I implemented before that sorted the numerical column incorrectly.

  11. #11
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Is it possible to sort a listview?

    I just plugged it into a project with the numbers you listed and it works fine. You are clicking on the column header to sort them right?

    If you are expecting them to be sorted when you load them you need to do that when gathering data or set it on the properties of the listview.

    I'm not trying to be cute...just asking.
    Last edited by TysonLPrice; Nov 17th, 2006 at 06:07 AM.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Is it possible to sort a listview?

    Quote Originally Posted by TysonLPrice
    I just plugged it into a project with the numbers you listed and it works fine. You are clicking on the column header to sort them right?

    If you are expecting them to be sorted when you load them you need to do that when gathering data or set it on the properties of the listview.

    I'm not trying to be cute...just asking.
    They're already sorted correctly when they load; the listview is supposed to show the two columns of data in Teams order, not Cases (a number) order.

    If I implement this code it 'works' but it doesn't, if that makes sense. It sorts the Team (alpha) list just fine. However, it sorts the numbers incorrectly, i.e. it sorts them as if they are text instead of numbers. I did say this once already.

    I also tried to implement the code that gavio referred to. I noted that I was having problems with it that I still have not worked out the solution to, and nobody has offered to step in and help with that. So I guess my app won't have a sortable listview; it isn't the end of the world.

  13. #13
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Is it possible to sort a listview?

    Don't know what you mean... Please give examples and possibly code.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Is it possible to sort a listview?

    Quote Originally Posted by randem
    Don't know what you mean... Please give examples and possibly code.
    My listview has two columns. One column has a list of teams. The other column has the number of cases associated with each team. I don't know any other way to explain it. Say like you have a sequence of numbers...1, 2, 4, 10, 15, 20, 35. When I sort by the number of cases, it is sequenced this way: 1, 10, 15, 2, 20, 35, 4. This is incorrect.

    This is the code that populates the listview when the form loads. It's probably crap, but it was my first and only attempt at using a listview. It loads fine, it just doesn't sort correctly. The code I was using to sort was the same one posted by TysonLPrice.

    VB Code:
    1. Public Sub PopTeamList()
    2.  
    3. Dim strSQL As String
    4. Dim rst As ADODB.Recordset
    5. Dim lstTeam As ListView              
    6. Dim itmx As ListItem
    7. Dim colx As ColumnHeader
    8.  
    9.     Call CheckConnection
    10.  
    11.     ' Source of the Team list
    12.     strSQL = "SELECT Team, Cases FROM vwFETeamList WHERE Team IS NOT NULL ORDER BY Team"
    13.  
    14.     Set lstTeam = CriteriaSelection.lstTeam
    15.    
    16.     With lstTeam.ColumnHeaders
    17.         Set colx = .Add(, , "Team")
    18.         Set colx = .Add(, , "Cases")
    19.     End With
    20.  
    21.     Set rst = New ADODB.Recordset
    22.     rst.Open strSQL, cnConn, adOpenForwardOnly, adLockReadOnly, adCmdText
    23.         lstTeam.Visible = False
    24.         Do While Not rst.EOF
    25.             Set itmx = lstTeam.ListItems.Add(, , rst.Fields(0))
    26.             itmx.SubItems(1) = rst.Fields(1)
    27.             rst.MoveNext
    28.         Loop
    29.         lstTeam.Visible = True
    30.  
    31.     lstTeam.View = lvwReport
    32.  
    33.     If rst.State = 1 Then
    34.         rst.Close
    35.     End If
    36.     Set rst = Nothing
    37.     Set lstTeam = Nothing
    38.     Set itmx = Nothing
    39.     Set colx = Nothing
    40.  
    41. End Sub

  15. #15
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Is it possible to sort a listview?

    Do you mean you want something like this?

    I believe MartinLiss gave me this code a long while ago...
    Attached Files Attached Files
    Last edited by randem; Nov 22nd, 2006 at 05:16 AM.

  16. #16
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Is it possible to sort a listview?

    I don't see in this code where you used the formatting I'm suggesting, several times... Try:

    itmx.SubItems(1) = Format$(rst.Fields(1), "@@@@@@@")

    This is for money:

    itmx.SubItems(1) = Format$(Format$(rst.Fields(1), "Currency"), "@@@@@@@")


  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Is it possible to sort a listview?

    Quote Originally Posted by TysonLPrice
    I don't see in this code where you used the formatting I'm suggesting, several times... Try:

    itmx.SubItems(1) = Format$(rst.Fields(1), "@@@@@@@")

    This is for money:

    itmx.SubItems(1) = Format$(Format$(rst.Fields(1), "Currency"), "@@@@@@@")

    I wasn't using formatting because I don't know how. First time using a listview.

  18. #18
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Is it possible to sort a listview?

    Quote Originally Posted by disruptivehair
    I wasn't using formatting because I don't know how. First time using a listview.
    Did it work?

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Is it possible to sort a listview?

    Quote Originally Posted by TysonLPrice
    Did it work?
    Yes, now that I've added it it does work. Thank you. I don't mean to appear short, I've just been too busy recently to give this my full attention; it fell quite low on my priority list!

  20. #20
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Is it possible to sort a listview?

    Quote Originally Posted by disruptivehair
    Yes, now that I've added it it does work. Thank you. I don't mean to appear short, I've just been too busy recently to give this my full attention; it fell quite low on my priority list!
    Great! There are limitations to that fix with negative numbers. VB puts a sign in front of them and then you get the alpha sorting problem again. Some of those other links address that but for whole numbers what I suggested (I got it from searching here) works fine.

    If you feel this is resolved edit your original post and add "Resolved" to the title so people will stop trying to work it.


  21. #21
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: [RESOLVED] Is it possible to sort a listview?

    Getting the feeling of being ignored.....

    Guess my help is not needed here

  22. #22
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: [RESOLVED] Is it possible to sort a listview?

    Quote Originally Posted by randem
    Getting the feeling of being ignored.....

    Guess my help is not needed here
    randem,

    The poster marked it resolved.

  23. #23
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: [RESOLVED] Is it possible to sort a listview?

    Quote Originally Posted by TysonLPrice
    randem,

    The poster marked it resolved.
    No kidding... I'm the one that can read the post... remember

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: [RESOLVED] Is it possible to sort a listview?

    Quote Originally Posted by randem
    No kidding... I'm the one that can read the post... remember
    Randem, I did download the code you offered and tried it; I had the same problems with the icons that I had earlier in the thread. Tyson's solution worked and was very simple, i.e. it required the least amount of effort to make the change, so I used it. I do appreciate everybody's help, though. I'm sorry you felt ignored.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width