Simple Question About Sorting a ListView
Okay, so I have an application that has a listview on it that I have set to sort column based on clicking a column header. I'm using the ColumnSort class from MSDN with this code in the form class:
Code:
Private Sub lstGroup1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lstGroup1.ColumnClick
' Determine if the clicked column is already the column that is
' being sorted.
If (e.Column = lvwColumnSorter.SortColumn) Then
' Reverse the current sort direction for this column.
If (lvwColumnSorter.Order = SortOrder.Ascending) Then
lvwColumnSorter.Order = SortOrder.Descending
Else
lvwColumnSorter.Order = SortOrder.Ascending
End If
Else
' Set the column number that is to be sorted; default to ascending.
lvwColumnSorter.SortColumn = e.Column
lvwColumnSorter.Order = SortOrder.Ascending
End If
' Perform the sort with these new sort options.
Me.lstGroup1.Sort()
End Sub
Now, this form has 13 different listviews on it that serve different functions and I am moving items between these listviews quite frequently. The problem I am running into is that if I have items sorted in a listview, I move some of the first items to a different listview and (for any given reason) move them back to the first, it tacks them on at the end, out of order.
( I have 1,2,3,4,5 in LV1 and move 1,2,3 to LV2 so I have: 1,2,3 in LV2 and 4,5 in LV1. Move 1,2,3 back to LV1 and it comes out 4,5,1,2,3 in LV1.)
Of course, I can just click the column headers to resort them, but with how much transferring is going on, that could be an awful lot of column clicks. So what I would like to do is sort the listviews, automatically in code, each time one of these transfers is made.
I apologize if this question has been answered (and it probably has) but it seems everyone I search wants to sort a listview on column click instead of what I need.
So, if I want to do this automatically, I can either modify the code above to handle it by changing e to what I need, or I can call the code above each time I need it by calling the event and passing the sender and e values to it. I would prefer the first option though. So my question is rather simple (I hope) if I put the sort method into its own sub, what do I substitute for e.Column to make this work automatically?
Re: Simple Question About Sorting a ListView
you should make reference to the column headers
you may not be able to do so directly so you must store a value that tels yo the column that is controlling the sort and the direction of the sort
so I would create 2 variables sorting_column ancd sorting_direction
other values may also be inportand when setting up the sorting if so create suitable variables for them
when ever you add to the list check these variables and force a resorting of the list
their may be an event available for listboxes ( so check )
here to help
Re: Simple Question About Sorting a ListView
Thanks incidentals, I got that automation working great.
To keep from starting a new thread I figured I would repost in this one.
I am now having issues automatically sorting a listview that is not displayed on the form. Using the lvwColumnSorter class from MSDN (like above), I have it set up the same way I did when I was sorting cloumns on a visible listview.
So I have listview1 that I am using as a master list so a user can manipulate the listview shown on the form then revert back to the original list. The problem is, using the lvwColumnSorter class, I cant get it to sort. First I create listview1 on form_load, then I add columns to it using the Columns.Add method. Then, during code execution, I have this:
Code:
lvwColumnSorter = New ListViewColumnSorter()
listview1.ListViewItemSorter = lvwColumnSorter
lvwColumnSorter.SortColumn = 2
lvwColumnSorter.Order = SortOrder.Ascending
listview1.Sort()
But it will not sort. No matter what values I manipulate, I can't get listview1 to sort one way or the other. Does the Sort() method only work on visible listviews or am I just making some odd mistake?
Update: in debugging, I've found that the above code is not calling the comparer method of the lvwColumnSorter Class. So if anyone can tell me why it's not calling it/how to call it, it would be a great help.