Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listview sorting by date and hour 1

Status
Not open for further replies.

macl

Programmer
Sep 15, 2004
65
DE
Hi all
i have a question
I have this listview that contains a name of a text and also a column containing the date and time the text has been created
what i now want to do is, that if a user clicks on the column the listview items should be sorted by that date-time column
does somebody know a easy way how i can realise that?
Thank you :)
 
Is your issue that you need to know how to sort by an arbitary column in the listview, or that sorting by date-time doesn't quite do what you expect?
 
the later one
if i let it sort with
lvwNotes.SortKey = ColumnHeader.Index - 1
then that works great for sorting the textname if the user clicks on that column but if he chooses to sort the listview after the second column which contains the date and time then this sorting doesnt work correctly
 
Is it just my impresion or the list view control columns are considered all as text? I admit I could be wrong, so don't take my word on it!
I believe that you fill this list view from a recordset. So, you can order the recordset by the "date" column when the user clicks the column header, clear the list view and reload it.

Hope I've been helpful,
Bogdan Muresan.
 
yes i think all items are placed there as strings, i guess thats why the date sorting doesnt work correctly
yes i get my data from a recordset
i already thought about sorting it in the recordset and reloading the treeview but is that not time-consuming? and is there no other way i can just sort it without having to reload my listview?
 
Time consuming?... If you count in milliseconds, yeah...
I've used some time ago this aproach and didn't have problems regarding speed. I meen, with a list view of 5000 rows or so.
I don't have any idea right now. No API that I could think of...

Hope I've been helpful,
Bogdan Muresan.
 
:D ok i guess i cant count all that fast after all ;)
yes i think i will try that then thank you :)
have a good day
 
The problem is that the LVW only does textual sorting. There are two ways around this.

1) Use the "SendMessage" API with "LVM_SORTITEMS"--which I've found to be pretty slow.

2) Create an additional hidden column in the LVW and when your date column is clicked, copy the dates to the hidden column--but use the Format$() function with a format string that will put the dates in a workable format ("YYYYMMDDHhNnSs" for instance). Then sort on THAT column and clear out the sorting dates. This is surprisingly fast, even with a few thousand ITMs--quicker than the "SendMessage"/"LVW_SORTITEMS" method, which you can find an example of in the MSDN. Also, if you use the "LockWindowUpdate" before and after this operation is performed, the LVW won't be refreshed a bunch of times and it'll run even faster. You may run into problem with users being able to resize the hidden column, but you can disallow this action through subclassing the LVW's sort ColumnHeader.
 
>the later one

Yes, I suspected as much, which is why I asked the question. SleepDepD has sneaked in with an apprpriate answer, although I don't completely agree with their view that LVM_SORTITEMS is slow (it depends just how much sorting is necessary); it is worth pointing out that the MSDN example is flawed, and only works well for small lists. thread222-578008 might be worth a look
 
Hi thanks for your help :)
i would want to try the method SleepDepD suggested but im just beginning with programming and didnt realy understand how i can do it :(
do i have to get one data item after the other and formate it to a date and then put it back onto the hidden column? but wont it then be a string again?
how do i do the formating?
and if the column data is in the right format can i do the sorting the same way i sort if the user clicks to have it sorted after the text? or do i have to do a different sorting?
thanks so much for help :)
 
another question how do i copy the items from the frist column into the second?
thanks :)
 
i found out it how to copy and formate it i do that with that code now

i = 1
n = lvw.ListItems.Count + 1
Do While i < n
lvw.SelectedItem = lvw.ListItems.Item(i)
strdate = lvwNotes.SelectedItem.SubItems(2)

lvw.SelectedItem.SubItems(3) = Format(strdate, "yy-mm-dd hh:nn")
i = i + 1
Loop
The sorting works :)
thanks for your help :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top