Results 1 to 4 of 4

Thread: Problem sorting list view [UnResolved]

  1. #1

    Thread Starter
    Addicted Member Porsche944's Avatar
    Join Date
    Apr 2005
    Location
    Ann Arbor
    Posts
    182

    Resolved Problem sorting list view [UnResolved]

    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:
    1. returnVal = [String].Compare(CType(x, _
    2.                         ListViewItem).SubItems(col).Text, _
    3.                         CType(y, ListViewItem).SubItems(col).Text)

    my class for the listview item comparer is this

    VB Code:
    1. Class ListViewItemComparer
    2.     Implements IComparer
    3.     Private col As Integer
    4.     Private order As SortOrder
    5.  
    6.     Public Sub New()
    7.         col = 0
    8.         order = SortOrder.Ascending
    9.     End Sub
    10.  
    11.     Public Sub New(ByVal column As Integer, ByVal order As SortOrder)
    12.         col = column
    13.         Me.order = order
    14.     End Sub
    15.  
    16.     Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
    17.                         Implements System.Collections.IComparer.Compare
    18.  
    19.         'Try
    20.         Dim returnVal As Integer = -1
    21.         returnVal = [String].Compare(CType(x, _
    22.                         ListViewItem).SubItems(col).Text, _
    23.                         CType(y, ListViewItem).SubItems(col).Text)
    24.         ' Determine whether the sort order is descending.
    25.         If order = SortOrder.Descending Then
    26.             ' Invert the value returned by String.Compare.
    27.             returnVal *= -1
    28.         End If
    29.  
    30.  
    31.  
    32.         Return returnVal
    33.         'Catch
    34.         'End Try
    35.     End Function
    36. End Class


    Here is the code inside my Listviews Column Click Event
    VB Code:
    1. ' Determine whether the column is the same as the last column clicked.
    2.         If e.Column <> sortColumn Then
    3.             ' Set the sort column to the new column.
    4.             sortColumn = e.Column
    5.             ' Set the sort order to ascending by default.
    6.             lvResults.Sorting = SortOrder.Ascending
    7.         Else
    8.             ' Determine what the last sort order was and change it.
    9.             If lvResults.Sorting = SortOrder.Ascending Then
    10.                 lvResults.Sorting = SortOrder.Descending
    11.             Else
    12.                 lvResults.Sorting = SortOrder.Ascending
    13.             End If
    14.         End If
    15.         ' Call the sort method to manually sort.
    16.         lvResults.Sort()
    17.         ' Set the ListViewItemSorter property to a new ListViewItemComparer
    18.         ' object.
    19.         lvResults.ListViewItemSorter = New ListViewItemComparer(e.Column, lvResults.Sorting)
    20.         If lvResults.Items.Count > 1 Then
    21.             'some varibles that are created from the drawing.color class
    22.             Dim white As System.Drawing.Color
    23.             Dim grey As System.Drawing.Color
    24.             white = Color.White
    25.             grey = Color.LightGray
    26.             'paint the rows Hurray took awhile to figure that one out
    27.  
    28.             PaintAlternatingBackColor(lvResults, white, grey)
    29.         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 ?
    Last edited by Porsche944; May 19th, 2005 at 03:35 PM.

  2. #2

    Thread Starter
    Addicted Member Porsche944's Avatar
    Join Date
    Apr 2005
    Location
    Ann Arbor
    Posts
    182

    Re: Problem sorting list view

    I fixed it , changing the sorting propertty to none before new search is run solved the problem.

    DUUUUH

  3. #3

    Thread Starter
    Addicted Member Porsche944's Avatar
    Join Date
    Apr 2005
    Location
    Ann Arbor
    Posts
    182

    Exclamation Re: Problem sorting list view [UnResolved]

    I'm still having this problem. How can I change that function thats crashing on my to include a if statement to check to make sure there is at least 2 items in the list view before it attempts to sort?

  4. #4

    Thread Starter
    Addicted Member Porsche944's Avatar
    Join Date
    Apr 2005
    Location
    Ann Arbor
    Posts
    182

    Re: Problem sorting list view [UnResolved]

    Bump

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