My sorting function for my list view will sort a column. There is no problem with that. The problem comes up when I clear out all the list view items and start adding new items.
As soon as I add an item I get this error
"An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll
Additional information: Specified argument was out of the range of valid values."
which happens in this line
VB Code:
returnVal = [String].Compare(CType(x, _ ListViewItem).SubItems(col).Text, _ CType(y, ListViewItem).SubItems(col).Text)
my class for the listview item comparer is this
VB Code:
Class ListViewItemComparer Implements IComparer Private col As Integer Private order As SortOrder Public Sub New() col = 0 order = SortOrder.Ascending End Sub Public Sub New(ByVal column As Integer, ByVal order As SortOrder) col = column Me.order = order End Sub Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _ Implements System.Collections.IComparer.Compare 'Try Dim returnVal As Integer = -1 returnVal = [String].Compare(CType(x, _ ListViewItem).SubItems(col).Text, _ CType(y, ListViewItem).SubItems(col).Text) ' Determine whether the sort order is descending. If order = SortOrder.Descending Then ' Invert the value returned by String.Compare. returnVal *= -1 End If Return returnVal 'Catch 'End Try End Function End Class
Here is the code inside my Listviews Column Click Event
VB Code:
' Determine whether the column is the same as the last column clicked. If e.Column <> sortColumn Then ' Set the sort column to the new column. sortColumn = e.Column ' Set the sort order to ascending by default. lvResults.Sorting = SortOrder.Ascending Else ' Determine what the last sort order was and change it. If lvResults.Sorting = SortOrder.Ascending Then lvResults.Sorting = SortOrder.Descending Else lvResults.Sorting = SortOrder.Ascending End If End If ' Call the sort method to manually sort. lvResults.Sort() ' Set the ListViewItemSorter property to a new ListViewItemComparer ' object. lvResults.ListViewItemSorter = New ListViewItemComparer(e.Column, lvResults.Sorting) If lvResults.Items.Count > 1 Then 'some varibles that are created from the drawing.color class Dim white As System.Drawing.Color Dim grey As System.Drawing.Color white = Color.White grey = Color.LightGray 'paint the rows Hurray took awhile to figure that one out PaintAlternatingBackColor(lvResults, white, grey) End If
What I'd like to do is every time I add an item to the list view (which is when you run a search) is make it so the program will no attempt to sort the column until the user clicks on the column.
For some reason it tries to sort anything that is added to the list view automaticly. How can I prevent it from doing this to me ?


Reply With Quote