-
[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?
-
Re: Is it possible to sort a listview?
Here's a very good sorting sample ;)
-
Re: Is it possible to sort a listview?
I do this:
In a module:
VB Code:
Public Sub SortListView(lvwList As MSComctlLib.ListView, ByVal ColumnHeader As MSComctlLib.ColumnHeader)
If lvwList.SortKey = (ColumnHeader.Index - 1) Then
If lvwList.SortOrder = lvwAscending Then
lvwList.SortOrder = lvwDescending
Else
lvwList.SortOrder = lvwAscending
End If
lvwList.Sorted = True
Else
lvwList.SortKey = (ColumnHeader.Index - 1)
lvwList.Sorted = True
lvwList.SortOrder = lvwAscending
End If
End Sub
In the form:
VB Code:
Private Sub lvwBills_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
SortListView lvwBills, ColumnHeader
End Sub
It has the alpha search limitation that gavio's post looks like it takes care of.
-
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.
-
Re: Is it possible to sort a listview?
Quote:
Originally Posted by TysonLPrice
I do this:
In a module:
VB Code:
Public Sub SortListView(lvwList As MSComctlLib.ListView, ByVal ColumnHeader As MSComctlLib.ColumnHeader)
If lvwList.SortKey = (ColumnHeader.Index - 1) Then
If lvwList.SortOrder = lvwAscending Then
lvwList.SortOrder = lvwDescending
Else
lvwList.SortOrder = lvwAscending
End If
lvwList.Sorted = True
Else
lvwList.SortKey = (ColumnHeader.Index - 1)
lvwList.Sorted = True
lvwList.SortOrder = lvwAscending
End If
End Sub
In the form:
VB Code:
Private Sub lvwBills_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
SortListView lvwBills, ColumnHeader
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. :(
-
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.
-
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.
-
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. :(
-
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:
Option Explicit
Private iItem As ListItem
Private Sub Form_Load()
Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
iItem.SubItems(1) = Format$("1", "@@@@@@@@@@@")
Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
iItem.SubItems(1) = Format$("11", "@@@@@@@@@@@")
Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
iItem.SubItems(1) = Format$("155", "@@@@@@@@@@@")
Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
iItem.SubItems(1) = Format$("2", "@@@@@@@@@@@")
Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
iItem.SubItems(1) = Format$("6", "@@@@@@@@@@@")
End Sub
Public Sub SortListView(lvwList As MSComctlLib.ListView, ByVal ColumnHeader As MSComctlLib.ColumnHeader)
If lvwList.SortKey = (ColumnHeader.Index - 1) Then
If lvwList.SortOrder = lvwAscending Then
lvwList.SortOrder = lvwDescending
Else
lvwList.SortOrder = lvwAscending
End If
lvwList.Sorted = True
Else
lvwList.SortKey = (ColumnHeader.Index - 1)
lvwList.Sorted = True
lvwList.SortOrder = lvwAscending
End If
End Sub
Private Sub lvwEMailEvents_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
On Error GoTo ErrorHandler
SortListView lvwEmailEvents, ColumnHeader
Exit Sub
ErrorHandler:
Screen.MousePointer = vbNormal
MsgBox "Error lvwEMailEvents_ColumnClick at Line # " & Erl & " (" & Err.Description & ")", , "Error: lvwEMailEvents_ColumnClick"
End Sub
-
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:
Option Explicit
Private iItem As ListItem
Private Sub Form_Load()
Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
iItem.SubItems(1) = Format$("1", "@@@@@@@@@@@")
Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
iItem.SubItems(1) = Format$("11", "@@@@@@@@@@@")
Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
iItem.SubItems(1) = Format$("155", "@@@@@@@@@@@")
Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
iItem.SubItems(1) = Format$("2", "@@@@@@@@@@@")
Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
iItem.SubItems(1) = Format$("6", "@@@@@@@@@@@")
End Sub
Public Sub SortListView(lvwList As MSComctlLib.ListView, ByVal ColumnHeader As MSComctlLib.ColumnHeader)
If lvwList.SortKey = (ColumnHeader.Index - 1) Then
If lvwList.SortOrder = lvwAscending Then
lvwList.SortOrder = lvwDescending
Else
lvwList.SortOrder = lvwAscending
End If
lvwList.Sorted = True
Else
lvwList.SortKey = (ColumnHeader.Index - 1)
lvwList.Sorted = True
lvwList.SortOrder = lvwAscending
End If
End Sub
Private Sub lvwEMailEvents_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
On Error GoTo ErrorHandler
SortListView lvwEmailEvents, ColumnHeader
Exit Sub
ErrorHandler:
Screen.MousePointer = vbNormal
MsgBox "Error lvwEMailEvents_ColumnClick at Line # " & Erl & " (" & Err.Description & ")", , "Error: lvwEMailEvents_ColumnClick"
End Sub
That's the code I implemented before that sorted the numerical column incorrectly. :(
-
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. :wave:
-
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. :wave:
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.
-
Re: Is it possible to sort a listview?
Don't know what you mean... Please give examples and possibly code.
-
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:
Public Sub PopTeamList()
Dim strSQL As String
Dim rst As ADODB.Recordset
Dim lstTeam As ListView
Dim itmx As ListItem
Dim colx As ColumnHeader
Call CheckConnection
' Source of the Team list
strSQL = "SELECT Team, Cases FROM vwFETeamList WHERE Team IS NOT NULL ORDER BY Team"
Set lstTeam = CriteriaSelection.lstTeam
With lstTeam.ColumnHeaders
Set colx = .Add(, , "Team")
Set colx = .Add(, , "Cases")
End With
Set rst = New ADODB.Recordset
rst.Open strSQL, cnConn, adOpenForwardOnly, adLockReadOnly, adCmdText
lstTeam.Visible = False
Do While Not rst.EOF
Set itmx = lstTeam.ListItems.Add(, , rst.Fields(0))
itmx.SubItems(1) = rst.Fields(1)
rst.MoveNext
Loop
lstTeam.Visible = True
lstTeam.View = lvwReport
If rst.State = 1 Then
rst.Close
End If
Set rst = Nothing
Set lstTeam = Nothing
Set itmx = Nothing
Set colx = Nothing
End Sub
-
1 Attachment(s)
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...
-
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"), "@@@@@@@")
:wave:
-
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"), "@@@@@@@")
:wave:
I wasn't using formatting because I don't know how. First time using a listview.
-
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?
-
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! :)
-
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.
:wave:
-
Re: [RESOLVED] Is it possible to sort a listview?
Getting the feeling of being ignored..... :sick:
Guess my help is not needed here :cool:
-
Re: [RESOLVED] Is it possible to sort a listview?
Quote:
Originally Posted by randem
Getting the feeling of being ignored..... :sick:
Guess my help is not needed here :cool:
randem,
The poster marked it resolved. :wave:
-
Re: [RESOLVED] Is it possible to sort a listview?
Quote:
Originally Posted by TysonLPrice
randem,
The poster marked it resolved. :wave:
No kidding... I'm the one that can read the post... remember :eek:
-
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 :eek:
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.