Salim32 Level: Big Cheese Registered: 24-09-2005 Posts: 21
How to sort a ListView column by Date and Time
Hi all,
Did someone know how to sort items in a ListView column by date and time.
Items are in format : 25/09/2005 3:07:00
and 2/10/2005 20:03:02
1/10/2005 22:05:07
That will be great if someone can help
Thanks in advance
____________________________
Salim
29-09-2005 at 09:51 PM
|
stickleprojects Level: Moderator Registered: 09-09-2002 Posts: 891
Re: How to sort a ListView column by Date and Time
Hi,
apart from listview... any chance of sorting them from your data source?
Kieron
____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)
29-09-2005 at 11:35 PM
|
Salim32 Level: Big Cheese Registered: 24-09-2005 Posts: 21
Re: How to sort a ListView column by Date and Time
Hi Maestro,
yeah this is global idea of the project:
file names created are in format "dd mmm yy hh mm ss"
each file contain a string date of a DTPicker:
"dd/mm/yyyy hh:mm:ss" Here is the problem i think
coz when the prog load and read that line it just return
1/10/2005 22:05:07 date and time and NOT
01/10/2005 22:05:07
and then ListView fail to sort correctly those Dates or that text.
and returns 1/10/2005 and time
1/10/2005 and time
2/10/2005 and time
with 30/09/2005 and time
OR i want this following order absolutely in my ListView:
30/09/2005 and time
1/10/2005 and time
1/10/2005 and time
2/10/2005 and time
Thanks Kieron
____________________________
Salim
30-09-2005 at 12:25 AM
|
TJ_01 Level: VB Lord Registered: 24-08-2005 Posts: 320
Re: How to sort a ListView column by Date and Time
Hi. You can use API in order to sort listview.
Private Sub Form_Load()
Dim oListItem As MSComctlLib.ListItem
Dim d As Double
ListView1.View = lvwReport
With ListView1.ColumnHeaders
.Add Text:="Number"
.Add Text:="Date"
End With
' Fill some data in listview
With ListView1.ListItems
Do While .Count < 1000
Set oListItem = .Add(Text:="Item: " & .Count + 1)
d = Rnd * 37300 ' Add some random dates
oListItem.ListSubItems.Add(Text:=CDate(d)).Tag = d
Loop
End With
End Sub
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
Dim oListItem As MSComctlLib.ListItem
If ColumnHeader.Index = 2 Then
For Each oListItem In ListView1.ListItems
oListItem.SubItems(1) = _
Format$(oListItem.ListSubItems(1).Tag, _
"yyyymmddHHMMSS")
Next oListItem
End If
With ListView1
.SortKey = ColumnHeader.Index - 1
.Sorted = True
If .SortOrder = lvwAscending Then
.SortOrder = lvwDescending
Else
.SortOrder = lvwAscending
End If
End With
If ColumnHeader.Index = 2 Then
For Each oListItem In ListView1.ListItems
oListItem.SubItems(1) = _
CDate(oListItem.ListSubItems(1).Tag)
Next oListItem
End If
End Sub
or you can try this one also:
Sort the Item by clicking columnHeader
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
If ListView1.Sorted And _
ColumnHeader.Index - 1 = ListView1.SortKey Then
ListView1.SortOrder = 1 - ListView1.SortOrder
Else
ListView1.SortOrder = lvwAscending
ListView1.SortKey = ColumnHeader.Index - 1
End If
ListView1.Sorted = True
End Sub
[Edited by TJ_01 on 30-09-2005 at 12:49 AM GMT]
[Edited by TJ_01 on 30-09-2005 at 12:49 AM GMT]
____________________________
Im JAMES
30-09-2005 at 12:40 AM
|
Salim32 Level: Big Cheese Registered: 24-09-2005 Posts: 21
Re: How to sort a ListView column by Date and Time
Hi James,
i c but my prog works in background in taskbar so i cant use this code coz it need to click to arrange items.
I want them to be arranged on load and in the startup of the prog.
All i need is that shitty automation to sort items by Date automatically when prog loads
Thanks James
____________________________
Salim
30-09-2005 at 01:44 AM
|
Salim32 Level: Big Cheese Registered: 24-09-2005 Posts: 21
Re: How to sort a ListView column by Date and Time
Please can someone Help ?.
____________________________
Salim
30-09-2005 at 08:10 PM
|
Salim32 Level: Big Cheese Registered: 24-09-2005 Posts: 21
Re: How to sort a ListView column by Date and Time
i GOT IT and no one else over all forums did it before.
It's soo tricky solution
just make the date and time format in new column like this:
a variable that gets year and month and day and Time value so u get this yyyymmdd
u must add the time in format : hhmmss
so u get yyyymmddhhmmss variable that u add to ur SubItems and then set all to be sorted by that columnHeader Index -1
if u want it to load sorted just add in form load the click column function and it's done.
Yahooooo
Hope this helps somone else and i can give more code to him