Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort Listview in Code 1

Status
Not open for further replies.

Lynus

Technical User
Apr 12, 2003
69
0
0
I see hundreds of posts on how to sort a listview when the user clicks on a column but what if I have a listview and I want it sorted before the user sees it. What I want to do is after the listview is populated, I want to sort it and then display the form to the user. Anyone know how?
 
If the listview is populated from a recordset, then include an <ORDER BY> clause in the query.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
No. The entire database is being displayed. Here is the code:

CHRDB.RecordSource = "Skills"
CHRDB.Refresh
CHRDB.Recordset.Sort = "Availability"

Dim skilllistload As ListItem
Do
CHRData1.DataField = "SkillName"
Set skilllistload = SkillList.ListItems.Add(, , CHRData1.Text)
CHRData1.DataField = "KeyAbility"
skilllistload.SubItems(1) = CHRData1.Text
CHRData1.DataField = "Availability"
skilllistload.SubItems(6) = CHRData1.Text
CHRData1.DataField = "AbilMod"
skilllistload.SubItems(3) = CHRData1.Text
CHRData1.DataField = "Ranks"
skilllistload.SubItems(4) = CHRData1.Text
CHRData1.DataField = "MiscMod"
skilllistload.SubItems(5) = CHRData1.Text
CHRData1.DataField = "SkillMod"
skilllistload.SubItems(2) = CHRData1.Text

CHRDB.Recordset.MoveNext
Loop Until CHRDB.Recordset.EOF
 
The same way as when you click on a column...

If this is the basic code you use to do column click sorting...
Code:
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
 With ListView1
  If .SortKey <> ColumnHeader.Index - 1 Then
   .SortKey = ColumnHeader.Index - 1
   .SortOrder = lvwAscending
  Else
   If .SortOrder = lvwAscending Then
    .SortOrder = lvwDescending
   Else
    .SortOrder = lvwAscending
   End If
  End If
  .Sorted = -1
 End With
End Sub

Then Just use:
.Sorted = True
.SortKey to set the Column
.SortOrder to set the Direction


Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
In other words...
Code:
CHRDB.RecordSource = "Skills"
CHRDB.Refresh
CHRDB.Recordset.Sort = "Availability"

Dim skilllistload As ListItem
Do
    CHRData1.DataField = "SkillName"
    Set skilllistload = SkillList.ListItems.Add(, , CHRData1.Text)

    CHRData1.DataField = "KeyAbility"
    skilllistload.SubItems(1) = CHRData1.Text

    CHRData1.DataField = "Availability"
    skilllistload.SubItems(6) = CHRData1.Text

    CHRData1.DataField = "AbilMod"
    skilllistload.SubItems(3) = CHRData1.Text

    CHRData1.DataField = "Ranks"
    skilllistload.SubItems(4) = CHRData1.Text

    CHRData1.DataField = "MiscMod"
    skilllistload.SubItems(5) = CHRData1.Text

    CHRData1.DataField = "SkillMod"
    skilllistload.SubItems(2) = CHRData1.Text
                
    CHRDB.Recordset.MoveNext
Loop Until CHRDB.Recordset.EOF 
 
SkillList.Sorted = True
SkillList.SortKey = 6 'Sort Availability (column 6, 0 based)
'SkillList.SortKey = 0
SkillList.SortOrder = lvwDescending
'SkillList.SortOrder = lvwAscending

Good Luck,
Josh

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
That worked like a charm. I tried setting the sortkey in the object properties this morning but it kept complaining about it being the wrong format. Anyway, You get a star dude, thanks for the help.

~Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top