Results 1 to 17 of 17

Thread: Column sorting in ListView

  1. #1

    Thread Starter
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828

    Column sorting in ListView

    ello.

    i'm using a ListView control and i've pretty much got it sussed apart from the sorting bit. i've got the sorting working no problem, but in a lot of programs the sort buttons (columnHeaders) have these little embedded arrows on them indicating which way the column is sorted in...

    how do i do that??
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    VB Code:
    1. 'This Sort ListView routine requires the use of an Image Control with two images;
    2. 'An Up Arrow and A Down Arrow.   These arrows will appear on the sorted column when clicked
    3. 'Put an ImageList on your form. Add the icons to the ImageList that you want to use
    4. 'for the Up and Down Arrows. In the property page of the ListView control, under the
    5. 'ImageLists Tab, set the Column Header to the name of your ImageList.
    6. Private Sub SortListView(ByRef List As ListView, ColHeadIndex As Integer)
    7.    
    8.     Dim lcv As Long     'Loop Control Variable
    9.     With List
    10.         ' Make sure the Sorted property is set to true
    11.         .Sorted = True
    12.         ' Sort according to the colum that was clicked (off by one)
    13.         .SortKey = ColHeadIndex - 1
    14.         ' Does the column already have an icon?
    15.         If .ColumnHeaders(ColHeadIndex).Icon = 0 Then
    16.             'No, So we will assume this column is not sorted
    17.             ' Set to Ascending order
    18.             .SortOrder = lvwAscending
    19.             ' Set the ColumnHeader to be the Up Arrow
    20.             .ColumnHeaders(ColHeadIndex).Icon = "UpArrow"
    21.         ' Does the column have an UpArrow icon?
    22.         ElseIf .ColumnHeaders(ColHeadIndex).Icon = "UpArrow" Then
    23.             ' Yes, So the column is in Ascending order, switch to descending
    24.             ' Set the Column Icon to the Down Arrow
    25.             .ColumnHeaders(ColHeadIndex).Icon = "DownArrow"
    26.             ' Set the sort order to descending
    27.             .SortOrder = lvwDescending
    28.         Else
    29.             ' Otherwise sort into ascending order
    30.             ' Set to Ascending order
    31.             .SortOrder = lvwAscending
    32.             ' Set the ColumnHeader to be the Up Arrow
    33.             .ColumnHeaders(ColHeadIndex).Icon = "UpArrow"
    34.         End If
    35.         ' Remove any icon (presumably an arrow icon) from all other columns
    36.         ' For every Column in the ListView Control...
    37.         For lcv = 1 To List.ColumnHeaders.Count
    38.             ' Is the current column the clicked column?
    39.             If Not (lcv = ColHeadIndex) Then
    40.                 ' No, remove any icon it may have
    41.                 .ColumnHeaders(lcv).Icon = 0
    42.             End If
    43.         Next lcv
    44.         ' Refresh the display of the ListView Control
    45.         .Refresh
    46.     End With
    47. End Sub
    48.  
    49. Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    50. Call SortListView(ListView1, ColumnHeader.Index)
    51. End Sub

  3. #3

    Thread Starter
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828

    Thumbs up

    aren't code libraries just great

    cheers hack
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    In the immortal words of darre1...



    Who's yer daddy?

  5. #5

    Thread Starter
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  6. #6

    Thread Starter
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    is there a way to get the image to align to the right of the columnheader???
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  7. #7

    Thread Starter
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    basically i need the text to align to the left of the columnheader and the icon to the right of the columnheader, like the attached pic:
    Attached Images Attached Images  
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  8. #8
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Talking If dog = cat then does a sheep = giraffe?

    Errrr...nope, u can't do that with a listview I am afraid
    U will start to come across the limitations of microsofts controls.
    That's why there is a huge marget for creating custom controls out there
    Also, the sorting only sorts on strings
    If you have the following in a column :

    10
    34
    1
    4
    2
    5
    21

    and click on the columnheader to sort it, you will get:

    1
    10
    2
    21
    34
    4
    5



    Booooooooooooooooooooooooooo

    I have a module with some lovely API commands in it if you want?
    It has some public functions in there and to sort on a column u use:
    VB Code:
    1. ListViewSortOnDate lvwListView, ColumnHeader.Index
    2. ListViewSortOnString lvwListView, ColumnHeader.Index
    3. ListViewSortOnLong lvwListView, ColumnHeader.Index

    Although I have found a problem with this recently as when u sort, lvwListview.SelectedItem points at another item

  9. #9
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Talking My house is on fire...At least I'll be warm...

    Sorry, u mean text allignment...

    VB Code:
    1. lvwListView.ColumnHeaders.Add , "MODULE", "Module", 1900, lvwColumnLeft
    U can have the following for columnheader allignment
    • 0 - lvwColumnLeft
    • 1 - lvwColumnRight
    • 2 - lvwColumnCenter

    But u can't allign the icon! it just sits there

  10. #10

    Thread Starter
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    cheers wokka.

    i guess i could align the text to the right and leave the icon to the left
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  11. #11

    Thread Starter
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    ARGHH!

    1st column must be aligned ot the left according to VB. pants pants pants!!!!
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  12. #12
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Talking Arse sticks and candle, my fone is poo...

    Did u ever have a look at the S-Grid on vbAccelerator?
    Stuffs the listview into the ground
    U can do everything with that grid, and it's free...

  13. #13
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Talking I laf at your capacity to conduct gamma rays!

    Hahahahaha...U r now realising the mistake of using a listview
    The 1st column ALWAYS has to be Left alligned...Hahahaha.
    Watch out for SubItems and ListSubItems
    Each ListItem can have many ListSubItems, one for each column...Hahahaha. Gets messy

    ListSubItems are the OBJECT equivalent of SubItems.
    You can change color, icons, font, etc, whereas with SubItems u can't...The reason SubItems are there is coz that's how it was 1st designed and they had to keep it in there to support older apps which used it

    Sorry

  14. #14

    Thread Starter
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    glad i amuse you


    j/k
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  15. #15

  16. #16

    Thread Starter
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    j/k is shorthand for Just Kidding, or Joking.

    in wokkaland its shorthand for Jemima's Kebabhouse
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  17. #17

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