listview sort error

I keep getting index error when sorting listview
can anyone explain why

sort-error.png
I call with this  and works fine

        Dim colNum = 4
        Form1.ListView1.ListViewItemSorter = New ListViewItemComparer(colNum)


but when I call with this it fails

        Dim colNum = 2
        Form1.ListView3.ListViewItemSorter = New ListViewItemComparer(colNum)

this is the process I call it in

 Sub ComboCalcs()
        Form1.ListView3.Items.Clear()
        ComboCount = 0
        Dim MyIntArray As New List(Of Integer)
        Dim lplvin As ListViewItem = Nothing
        For Each Runner In Runners
            If Runner.Check1 = True Or Runner.Check2 = True Or Runner.Check3 = True Then
                MyIntArray.Add(Convert.ToInt32(Runner.Number))
            End If
        Next
        NumList = MyIntArray.ToArray
        Dim fp As Integer
        Dim sp As Integer
        Dim tp As Integer

        For Each combination In GetCombinations(NumList)
            With combination(0)
                For Each Runner In Runners
                    If Runner.Number = combination(0) And Runner.Check1 = True Then
                        fp = combination(0)
                        With combination(1)
                            For Each Runner1 In Runners
                                If Runner1.Number = combination(1) And Runner1.Check2 = True Then
                                    sp = combination(1)
                                    With combination(2)
                                        For Each Runner2 In Runners
                                            If Runner2.Number = combination(2) And Runner2.Check3 = True Then
                                                tp = combination(2)
                                                betAmt(fp, sp, tp)
                                                'Dim strCbo = String.Format("{0} - {1} - {2}", fp, sp, tp)
                                                'Dim objItem As ListViewItem = Form1.ListView3.Items.Add(String.Format("{0} - {1} - {2}", fp, sp, tp), 3)
                                                Dim objItem As ListViewItem = Form1.ListView3.Items.Add("")
                                                With objItem
                                                    .SubItems.Add(String.Format("{0} - {1} - {2}", fp, sp, tp))
                                                    .SubItems.Add(outlay.ToString)
                                                End With
                                                Form1.lblRisk.Text = "Risking $" & Risk & " to make $" & RP & " return"
                                                ComboCount += 1
                                            End If
                                        Next
                                    End With
                                End If
                            Next
                        End With
                    End If
                Next
            End With
        Next
        Risk = 0
        Dim colNum = 2
        Form1.ListView3.ListViewItemSorter = New ListViewItemComparer(colNum)
        Form1.lblNumCombo.Text = ComboCount
    End Sub

Open in new window

JasonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ammar GaffarSoftware EngineerCommented:
Your working code is dealing with ListView1 :
Dim colNum = 4
        Form1.ListView1.ListViewItemSorter = New ListViewItemComparer(colNum)

Open in new window

Your none working one is dealing with different List View (ListView3) :
Dim colNum = 2
        Form1.ListView3.ListViewItemSorter = New ListViewItemComparer(colNum)

Open in new window

Are you sure that ListView3 has 3 columns + ?
0
JasonAuthor Commented:
yes
listview
has 3 columns

listview3.png
0
Ammar GaffarSoftware EngineerCommented:
You are trying to implement custom sorting for your ListView, this customization requires all items must have the same sub items structure, in your case all items must have 3 sub items to represent your 3 columns in this case you should not face any issue.

Review your code block which you are using to add items and sub items, if one of your items doesn't have value for one column (sub item) then add it and fill it with empty string.
0
JasonAuthor Commented:
found this code to solve the problem
 Class ListViewComparer
        Implements IComparer

        Private m_ColumnNumber As Integer
        Private m_SortOrder As SortOrder

        Public Sub New(ByVal column_number As Integer, ByVal sort_order As SortOrder)
            m_ColumnNumber = column_number
            m_SortOrder = sort_order
        End Sub

        ' Compare the items in the appropriate column
        ' for objects x and y.
        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
            Dim item_x As ListViewItem = DirectCast(x, ListViewItem)
            Dim item_y As ListViewItem = DirectCast(y, ListViewItem)

            ' Get the sub-item values.
            Dim string_x As String
            If item_x.SubItems.Count <= m_ColumnNumber Then
                string_x = ""
            Else
                string_x = item_x.SubItems(m_ColumnNumber).Text
            End If

            Dim string_y As String
            If item_y.SubItems.Count <= m_ColumnNumber Then
                string_y = ""
            Else
                string_y = item_y.SubItems(m_ColumnNumber).Text
            End If

            ' Compare them.
            If m_SortOrder = SortOrder.Ascending Then
                If IsNumeric(string_x) And IsNumeric(string_y) Then
                    Return Val(string_x).CompareTo(Val(string_y))
                ElseIf IsDate(string_x) And IsDate(string_y) Then
                    Return DateTime.Parse(string_x).CompareTo(DateTime.Parse(string_y))
                Else
                    Return String.Compare(string_x, string_y)
                End If
            Else
                If IsNumeric(string_x) And IsNumeric(string_y) Then
                    Return Val(string_y).CompareTo(Val(string_x))
                ElseIf IsDate(string_x) And IsDate(string_y) Then
                    Return DateTime.Parse(string_y).CompareTo(DateTime.Parse(string_x))
                Else
                    Return String.Compare(string_y, string_x)
                End If
            End If
        End Function
    End Class

Open in new window


and this to initiate sort

 Dim colNum = 0
        Dim new_sorting_column As ColumnHeader = Form1.ListView3.Columns(colNum)
        Form1.ListView3.ListViewItemSorter = New ListViewComparer(colNum, SortOrder.Descending)
        Form1.ListView3.Sort()
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JasonAuthor Commented:
assistance did not really help
thank for you efforts though
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.