|
-
Apr 13th, 2005, 12:18 PM
#1
Thread Starter
Frenzied Member
Need some list view help.. Please :) -> Resolved
I am using a list view with several columns in it. I need to be able to sort all the rows based on a user selected column.. Exactly like in Windows Explorer where if you click on "Name" it sorts by name, click on it again and it sorts in the other direction, and the same for all other columns..
Last edited by RudyL; Apr 25th, 2005 at 10:26 AM.
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Apr 13th, 2005, 12:48 PM
#2
Thread Starter
Frenzied Member
Re: Need some list view help.. Please :)
To add some more detail.. I have this example from M$, but it does not work..
http://msdn.microsoft.com/library/de...ortertopic.asp
I tried to berak pont in the sub that handlesthe column click event but it gives me a message that the breakpoint will never be hit..
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Apr 13th, 2005, 12:55 PM
#3
Re: Need some list view help.. Please :)
To get your breakpoint to be hit you need to run the project by pressing F5 or Debug > Run. If your
pressing the exclamation point toolbar button or menu item it will bypass breakpoints.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 13th, 2005, 01:02 PM
#4
Thread Starter
Frenzied Member
Re: Need some list view help.. Please :)
 Originally Posted by RobDog888
To get your breakpoint to be hit you need to run the project by pressing F5 or Debug > Run. If your
pressing the exclamation point toolbar button or menu item it will bypass breakpoints.
ahh... Not it, but you got me to see why.. I hat it set to Release still.. oopss.. However, It is still not working right.. It will sort the first time I click on it, but if I click again in the same column it will not sort the other way (asc/desc)..
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Apr 13th, 2005, 01:03 PM
#5
Re: Need some list view help.. Please :)
Hello,
Have you gone through and checked all the little bits, like you've definitely included the class ListViewItemComparer at the bottom, and the Imports System.Collections at the top?
The M$ example works fine for me.
zaza
-
Apr 13th, 2005, 01:11 PM
#6
Thread Starter
Frenzied Member
Re: Need some list view help.. Please :)
 Originally Posted by zaza
Hello,
Have you gone through and checked all the little bits, like you've definitely included the class ListViewItemComparer at the bottom, and the Imports System.Collections at the top?
The M$ example works fine for me.
zaza
I think so.
This is in the form..
VB Code:
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections
.......
Private Sub lvTestYields_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvTestYields.ColumnClick
' Set the ListViewItemSorter property to a new ListViewItemComparer
' object. Setting this property immediately sorts the
' ListView using the ListViewItemComparer object.
Me.lvTestYields.ListViewItemSorter = New ListViewItemComparer(e.Column)
End Sub
and I created a class with this in it..
VB Code:
Class ListViewItemComparer
' Implements the manual sorting of items by columns.
Implements IComparer
Private col As Integer
Public Sub New()
col = 0
End Sub
Public Sub New(ByVal column As Integer)
col = column
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
End Function
End Class
The class stuff wouldn't get put in the form would it?
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Apr 13th, 2005, 01:16 PM
#7
Re: Need some list view help.. Please :)
Hi,
In the columnclick event, after Me.blahblah = New blahblah, put:
lvtestyields.sort()
See if that works.
Fingers Xd
zaza
-
Apr 13th, 2005, 01:23 PM
#8
Thread Starter
Frenzied Member
Re: Need some list view help.. Please :)
 Originally Posted by zaza
Hi,
In the columnclick event, after Me.blahblah = New blahblah, put:
lvtestyields.sort()
See if that works.
Fingers Xd
zaza
Nope.. I just noticed another wierd thing.. I have 5 columns.. If I click on column 0 it sorts by column 0 asc.. If I click on column 1 it sorts by column 0 desc.. The same for columns 2 and 3, but they sort based on column 2..
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Apr 13th, 2005, 03:11 PM
#9
Re: Need some list view help.. Please :)
Hello,
Actually, having gone back to my sorter, it only sorts ascending. I'm sure I had it working before...
Anyway, try selecting some more data to put into your listview - I think it may just be coincidence that it appears to sort like that. I found a similar "phenomenon" until I changed what was going into the listview. The upshot of what I have found with regard to mine is that it more or less sorts in ascending order. Whichever column is clicked, it will sort mostly alphabetically according to that. By "mostly", I mean that the sorting occurs based only on what is in that column (although in the case of multiple items which are teh same, it sorts on the previous column), but it seems to have slightly odd way of choosing what comes first in the order. Square brackets come above curlies, which come above alphanumeric. I don't know why that is, because it doesn't reflect their ascii codes. In all other respects, though, it seems to sort in ascending order by ascii code based firstly on the column that you choose, then on the preceding column.
Thanks for making me look at my code again. As I say, I thought I'd sorted this out (hohoho). Now I'll have to look at it again.
Cheers
zaza
EDIT:
Ah yes, that's what I did. Create a second comparer class, the same as the first but swap the x and y in the "Return [string]" line. Then, use a flag (such as a public variable called "sortorder") to determine one of two courses of action in your columnclick event. If "sortorder" is 1, then use listviewitemcomparer1 and set "sortorder = -1". If "sortorder = -1", then use listviewitemcomparer2 and set "sortorder = 1". This swaps between the two versions of the sort, enabling you to sort ascending or descending each time you click.
HTH
zaza
Last edited by zaza; Apr 13th, 2005 at 03:26 PM.
-
May 21st, 2008, 12:06 PM
#10
Re: Need some list view help.. Please :) -> Resolved
you add a new class to your project and you paste in that code.
You then compile your application, and the new ListViewEx should be in your toolbox so you can just drag it to your form.
-
May 22nd, 2008, 03:57 AM
#11
Fanatic Member
Re: Need some list view help.. Please :) -> Resolved
Thank you kleinma..
Wouldnt know how to force that to a certain column.. on like the load... ??
I tried raising an colum click but that just ran into my listview column click event - (no code in it) and never hit the class.
Basically on my list view column 2 is like a date field and and i want to sort this as soon as default
Last edited by d2005; May 22nd, 2008 at 06:03 AM.
it works 60% of the time, all the time.
-
May 26th, 2008, 09:02 PM
#12
Re: Need some list view help.. Please :) -> Resolved
 Originally Posted by d2005
Basically on my list view column 2 is like a date field and and i want to sort this as soon as default
Where does the data come from? Likely it is in some sort of array or collection or database table already right? So sort it by date when you load the data, then load the listview with the already sorted data.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|