Is that sorting by the recordset query? If so, that will not work in my case as the data is not fed in on a column click. Thank you for your help though
Maybe I should have been more specific. It does fill from a record set when the user opens that screen but i do not wish to re-query the server for the record set. I just want to sort the data they already have.
This is a solution I use. In this example columns 3, 4 and 5 contain numberic data and the rest character data.
Private Sub lsvCutNumbers_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
With lsvCutNumbers
If ColumnHeader.Index >= 3 And ColumnHeader.Index <= 5 Then
For lngCount1 = 1 To .ListItems.Count
.ListItems(lngCount1).SubItems(ColumnHeader.Index - 1) = Format(.ListItems(lngCount1).SubItems(ColumnHeader.Index - 1), "000000000"
Next lngCount1
End If
If .SortKey = ColumnHeader.Index - 1 Then
If .SortOrder = lvwAscending Then
.SortOrder = lvwDescending
Else
.SortOrder = lvwAscending
End If
Else
.SortKey = ColumnHeader.Index - 1
End If
If ColumnHeader.Index >= 3 And ColumnHeader.Index <= 5 Then
For lngCount1 = 1 To .ListItems.Count
.ListItems(lngCount1).SubItems(ColumnHeader.Index - 1) = CLng(.ListItems(lngCount1).SubItems(ColumnHeader.Index - 1))
Next lngCount1
End If
If .ListItems.Count > 0 Then
.SelectedItem = .ListItems(1)
End If
tekomni
There are a couple of methods, but this may be the easiest, if there are only a few number/date columns:
1. When you add the elements and sub elements, add an additional block of sub elements at the end, which duplicates those columns.
These additional columns are hidden, and when added to the list, are formated as such:
Numbers: Format$(NumFld, "0000.0000" or similar, depending the number type and how many places are needed.
Dates: Format(DataFld, "yyyy-MM-dd"
When a user click a certain date field, you sort on the related HIDDEN date field.
Make sure the values are changed in both fields when the User, or the code, edits an element.
Another solution is to use this MS Kb article: Q170884
And yet another involves using a disconnected recordset and it's Sort method, and a hidden Sub Element column...
Sure. As I think I say in the referenced thread, my code is based on an MVPS example which in turn is based on the MS KB article (actually, it may be the other way around; the MS article may be derived from the MVPS code). However, both examples are flawed, in that they only work properly with a small number of items in the ListView, and the code in the referenced thread theoretically fixes this shortcoming
Thanks for your help. I used a mixture of the suggestions. I added a column at runtime, set the subitems to equal the item that was clicked formatted like 0000000.00, then sort the list by the new column then remove that column. Here is the code:
Public Sub SortList(ByRef myList As ListView, myColumn As Integer)
Dim column As Integer
Dim i As Integer
Dim lst As ListItem
column = myColumn - 1
'CHECK IF NUMBER
If myList.ListItems.Count > 0 Then
If Len(myList.ListItems(1).ListSubItems(myColumn).Text) > 3 And Val(myList.ListItems(1).ListSubItems(myColumn).Text) > 0 Then
If left(Right(myList.ListItems(1).ListSubItems(myColumn).Text, 3), 1) = "." Then
'ADD A HIDDEN COLUMN
myList.ColumnHeaders.Add , , "", 0
'SET THE SORT COLUMN = THE NEWLY CREATED COLUMN
column = myList.ColumnHeaders.Count - 1
For i = 1 To myList.ListItems.Count
Set lst = myList.ListItems(i)
'ADD ITEMS TO NEW COLUMN FORMATTED APPROPRIATELY
lst.ListSubItems.Add , , Format(myList.ListItems(i).ListSubItems(myColumn).Text, "000000.00"
Next i
End If
End If
End If
'SET SORT KEY
myList.SortKey = column
If myList.SortOrder = lvwDescending Then
myList.SortOrder = lvwAscending
Else
myList.SortOrder = lvwDescending
End If
'SORT THE LIST
myList.Sorted = True
'REMOVE THE NEW COLUMN
myList.ColumnHeaders.Remove (myList.ColumnHeaders.Count)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.