Results 1 to 37 of 37

Thread: Need some list view help.. Please :) -> Resolved

Hybrid View

  1. #1

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Resolved 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".

  2. #2

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    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".

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  4. #4

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Re: Need some list view help.. Please :)

    Quote 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".

  5. #5
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    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

  6. #6

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Re: Need some list view help.. Please :)

    Quote 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:
    1. Imports System
    2. Imports System.Windows.Forms
    3. Imports System.Drawing
    4. Imports System.Collections
    5.  
    6. .......
    7.  
    8.     Private Sub lvTestYields_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvTestYields.ColumnClick
    9.  
    10.         ' Set the ListViewItemSorter property to a new ListViewItemComparer
    11.         ' object. Setting this property immediately sorts the
    12.         ' ListView using the ListViewItemComparer object.
    13.         Me.lvTestYields.ListViewItemSorter = New ListViewItemComparer(e.Column)
    14.  
    15.     End Sub

    and I created a class with this in it..

    VB Code:
    1. Class ListViewItemComparer
    2.     ' Implements the manual sorting of items by columns.
    3.  
    4.     Implements IComparer
    5.  
    6.     Private col As Integer
    7.  
    8.     Public Sub New()
    9.  
    10.         col = 0
    11.  
    12.     End Sub
    13.     Public Sub New(ByVal column As Integer)
    14.  
    15.         col = column
    16.  
    17.     End Sub
    18.     Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
    19.  
    20.         Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
    21.  
    22.     End Function
    23. 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".

  7. #7
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    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

  8. #8

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Re: Need some list view help.. Please :)

    Quote 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".

  9. #9
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    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.

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  11. #11
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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.

  12. #12
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Need some list view help.. Please :) -> Resolved

    Quote 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
  •  



Click Here to Expand Forum to Full Width