 |
 |

10-20-2006, 02:49 PM
|
|
Centurion
|
|
Join Date: Jan 2006
Location: Finland
Posts: 114
|
|
ListView.Sort Trouble
|
Hello,
I think I've managed to find a bug in the ListView control. When I tell the ListView to sort, it only sorts the first group (lv1.groups(0)) and leaves the other group unsorted. There doesn't seem to be a way to sort all groups so what do I do now? 
|
|

10-20-2006, 08:13 PM
|
 |
Senior Contributor
Forum Leader * Expert *
|
|
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
|
|
Take a look at IComparer. I posted a link on a previous thread that has an example.
|
__________________
win7 : vs 2008 : .Net 3.5
|

10-21-2006, 08:28 AM
|
|
Centurion
|
|
Join Date: Jan 2006
Location: Finland
Posts: 114
|
|
Quote:
|
Originally Posted by IUnknown
Take a look at IComparer. I posted a link on a previous thread that has an example.
|
Ah I were hoping it could be done without that, just me missing something simple. I guess I'll make my own sorting function with IComparer then (I do not like copying). Thanks.
Edit: Manual sorting does not work on groups. The MS example does not use groups either (I wonder why  ). Now, is it really impossible to sort all groups in a listview control?
Edit: Tried to put the items for the 2 groups into 2 separate listviews and sort them there before cloning the items to the visible listbox. This time there was no sorting in any of the groups. No matter how I copied the items it just wouldn't get them in the sorted order.
|
Last edited by ShadowWolf; 10-21-2006 at 10:00 AM.
|

10-21-2006, 10:56 AM
|
 |
Senior Contributor
Forum Leader * Expert *
|
|
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
|
|
|
What are groups? Can you post a short example of what you're trying to achieve.
|
__________________
win7 : vs 2008 : .Net 3.5
|

10-21-2006, 12:59 PM
|
|
Centurion
|
|
Join Date: Jan 2006
Location: Finland
Posts: 114
|
|
Quote:
|
Originally Posted by IUnknown
What are groups? Can you post a short example of what you're trying to achieve.
|
Code:
ListView1.Groups.Add("g1","Group 1")
ListView1.Groups.Add("g2","Group 2")
Dim LVI as ListViewItem
LVI = ListView1.Items.Add("Test 1")
LVI.Group = ListView1.Groups(0)
LVI = ListView1.Items.Add("Test 2")
LVI.Group = ListView1.Groups(1)
ListView1.Sort() 'This only sorts ListView1.Groups(0) and there is no way to set the "active" group.
Edit: What I'm trying to create is a listview with folders, sorted alphabetically at the top, and files below them, also sorted alphabetically. With groups you can separate these 2, the problem is that the files are never sorted (ListView1.Group(1)).
|
|

10-21-2006, 03:43 PM
|
 |
Senior Contributor
Forum Leader * Expert *
|
|
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
|
|
|
If you implement IComparer, you can sort the data in the list pretty much anyway you choose as you control what value the Compare method will return. If you want to sort within each group, you would probably need to compare both the grouping and the columns. There is an example in the MSDN2 on sorting listview groups, look for ListViewGroup Class.
Hope this helps!
|
__________________
win7 : vs 2008 : .Net 3.5
|

10-22-2006, 04:58 AM
|
|
Centurion
|
|
Join Date: Jan 2006
Location: Finland
Posts: 114
|
|
Solution
I read that article on MSDN2 and weren't able to get it to do anything but remove all my groups and sort folders and files mixed up in alphabetical order. However, during the night I got this idea, which is alot more simple and flexible than what M$ seems to have to offer. This is approximately what I did:
Code:
Dim DirInfo As New DirectoryInfo(Folder)
Dim Dirs() As DirectoryInfo = DirInfo.GetDirectories()
Dim Files() As FileInfo = DirInfo.GetFiles(Filter)
Array.Sort(Dirs, New DiretoryInfoComparer) 'Custom made comparer
Array.Sort(Files, New FileInfoComparer) 'Custom made comparer
For Each D As DirectoryInfo In Dirs 'Do this for the files too
'Add them to a listview with no sorting turned on
Next
One of the comparers look like this: (Still a WIP, I will add support for sorting different columns and in different orders etc.)
Code:
Private Class DirectoryInfoComparer
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim result As Int32 = String.Compare(CType(x, DirectoryInfo).Name, CType(y, DirectoryInfo).Name)
Return result
End Function
End Class
Thanks for the help IUnknown, source for some of the ideas above were from the links you gave me. I hope the code above can help other people with the same problems. 
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|