Re: Ordering Date columns in ListViews
- From: "Dave" <nobody@xxxxxxxxxxx>
- Date: Fri, 21 Apr 2006 13:54:16 +0100
Hi
There are a zillion ways to do this, the easiest is to move to Japan where
the date format is YYYY MM DD which sorts correctly, however that may not be
suitable for everybody. There is a clever API method which works but fouls
the listview indexing so it can be tricky to identify an item correctly.
The method I use has several steps but is so fast you don't notice the
intermediate stages (unless you have 1,000's of items in the list). Before
you do this make sure sorting is turned off in the listview and the sortkey
is set to the desired column.
Step 1 Express the date as a number and pad with leading zeros so every date
returns a number as a string of the same length
Step 2 Add that number to the front of the date in the column
Step 3 Turn on the lv sort for that column then turn it off again, the colun
is now sorted correctly
Step 4 Trim the numbers from the front of each entry to leave the original
string
This is a base routine I use, it allows the date to be in the first column,
if that never happens you can strip out a lot of the code. I also allow for
blank colunms, if that will never happen you can also trim it down a bit. It
assumnes the dates are correctly formatted for CDate to cope with, if this
is not the case you'll need a bit of error checking.
Public Sub LVDateSort(dLV As ListView)
Dim i As Integer
Dim s As String
Dim LVsk As Integer
LVsk = dLV.SortKey
' Sort on dates - First replace the formatted date with a long date value
padded to
' 7 digits with the formatted date appended for later retrieval
If LVsk = 0 Then
For i = 1 To dLV.ListItems.Count
s = dLV.ListItems(i).Text
If s > "" Then dLV.ListItems(i).Text = Right$("00000" &
CStr(CLng(CDate(s))), 7) & s
Next
Else
For i = 1 To dLV.ListItems.Count
s = dLV.ListItems(i).SubItems(LVsk)
If s > "" Then dLV.ListItems(i).SubItems(LVsk) = Right$("00000" &
CStr(CLng(CDate(s))), 7) & s
Next
End If
' Now sort the long values into the correct order
dLV.Sorted = True
' Tun off the sorting but leave list sorted
dLV.Sorted = False
' now trim off the long values leaving the original formatted date correctly
sorted
If LVsk = 0 Then
For i = 1 To dLV.ListItems.Count
s = dLV.ListItems(i).Text
If s > "" Then dLV.ListItems(i).Text = Mid$(s, 8)
Next
Else
For i = 1 To dLV.ListItems.Count
s = dLV.ListItems(i).SubItems(LVsk)
If s > "" Then dLV.ListItems(i).SubItems(LVsk) = Mid$(s, 8)
Next
End If
End Sub
"Andy" <andrewjellis@xxxxxxxxxxxxxxxxx> wrote in message
news:85B31B6A-FDB6-47FE-B8B4-94873CA8FD15@xxxxxxxxxxxxxxxx
My app. has a number of ListViews using view = 3 (lvwReport), including
columns containing dates. The users need to have ther usual functionality
of
clicking on a column header and having the items ordered on that column.
Trouble is, the dates are displayed as text (obviously), and the order
doesn't get figured correctly. (We're working with UK-style date formats,
i.e. dd/mm/yyyy, and as an example, 01/01/2006 is alphabetically prior to
31/12/2005, therefore it gets displayed as an earlier item in the
ListView.)
If I could arrange for a hidden column containing a copy of the date but
in
the form yyyy/mm/dd, and order on that column instead, the problem would
be
fixed. However, I don't see how to actually 'hide' a column. If I simply
give it a width of 0, the user can easily bring it back into view by
resizing
it.
Has anyone seen this problem before? I'm damned sure you have! What's
the
workround?
Andy
.
- Follow-Ups:
- Re: Ordering Date columns in ListViews
- From: Andy
- Re: Ordering Date columns in ListViews
- Prev by Date: Re: VB6, VB2005, or Something Else?
- Next by Date: Re: VB6 IDE question
- Previous by thread: VB6 IDE question
- Next by thread: Re: Ordering Date columns in ListViews
- Index(es):
Relevant Pages
|