Click here to Skip to main content
14,330,512 members
Rate this:
Please Sign up or sign in to vote.
See more:
My current project includes an ownerdraw ListView with 6 columns used only in Details View. I rarely use the ListView so I'm likely just missing something obvious. I don't need to sort on each column. Sorting the entire list is sufficient (on col 0). The form loads with the ListView.Sorting property set to Ascending. I have a ComboBox to select sort order. I don't use None. Just ascending / descending.

But when I switch between ascending / descending all I get is a brief flicker and no change in sort order. Here's the code to switch the Sorting property:

Private Sub cmb_Sorting_SelectionChangeCommitted() _
    Handles cmb_Sorting.SelectionChangeCommitted
    Try
        Select Case cmb_Sorting.SelectedIndex
            Case 0
                lvFiles.Sorting = SortOrder.Ascending
            Case 1
                lvFiles.Sorting = SortOrder.Descending
        End Select

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub


What I have tried:

To be honest I wasn't sure what to try. I added a msgbox to show me the Sorting property each time I selected from the list and apparently the sort order is changing. But the change is not reflected in the control. Am I doing something wrong here?

Thanks - AB
Posted
Updated 19-May-19 7:14am
Comments
DerekT-P 17-May-19 11:34am
   
Can you provide your markup and server-side code, please; we'll need to understand when/how you're populating the listview etc.
Alan Burkhart 17-May-19 16:47pm
   
It's a Windows Forms desktop application.
1. Borrows cmd.exe briefly to get a list of directories, then searches file names and / or text for matches. This is done on a background thread.
2. Each dir is searched and if matches are found, a new listviewgroup (w/ the dir path as its text) is created and the items are assigned to the group.
DerekT-P 18-May-19 8:11am
   
I assume you've stepped through the code to verify the .Sorting method is actually being called? Have you defined any custom comparison method? Have you tried explicitly calling the .Sort() method after setting the sort sequence to force a re-sort? And have you verified that the form isn't reloading the data each time the combo box selection is changed?
(BTW, why use a separate thread with cmd.exe just to search some directories for matching filenames??)
Alan Burkhart 18-May-19 9:04am
   
As to using cmd.exe, it was a quick and easy way to get a list of directories without dealing with any access violations. And I should add that this isn't production code. It's just a utility for my own use. I don't do this for a living.

I tried to figure out the sorting problem by creating a separate project with nothing but a ListView and 2 RadioButtons to select the sorting order (items added in the Designer). Same result.

Public Class Form1
Private Sub RadioButton1_CheckedChanged() Handles RadioButton1.CheckedChanged
LV.Sorting = SortOrder.Ascending
End Sub

Private Sub RadioButton2_CheckedChanged() Handles RadioButton2.CheckedChanged
LV.Sorting = SortOrder.Descending
End Sub
End Class

My understanding is that setting the Sorting property is all that's required unless I'm performing a custom sort, which I'm not. I only need to sort the file names.
Rate this:
Please Sign up or sign in to vote.

Solution 2

We still haven't seen the code that populates your listview. In my simple test just changing the SortOrder does immediately redraw the listview in the new sequence. However it sorts on the .Text property of the item, not on the subitems (columns). Try setting the Item.Text value equal to the value in columns(0) and you should find all is well.
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

You're changing (or attempting) to change the sort order in an "event handler" which also "deals with the display". You probably have some event "contention".

You need to rethink why and when your app is changing the sort order (and not the user).
   
Comments
Alan Burkhart 17-May-19 18:51pm
   
I created a temp project consisting of 2 radiobuttons and a 2-column listview. In the form's load event I populated the listview by looping thru the FontFamilies on my machine. Col 0 got the names and col 1 was filled with the string length of the names. The radiobuttons were to select the sorting order. The results were the same. The listview blinked but did not re-sort. But the Sorting property is changing each time I click a radiobutton.

Please explain what you mean by "event contention." Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100