Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

listview sort error

Posted on 2014-12-20
5
142 Views
Last Modified: 2015-01-04
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

0
Comment
Question by:Jason
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:Ammar Gaffar
ID: 40511407
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
 

Author Comment

by:Jason
ID: 40511482
yes
listview
has 3 columns

listview3.png
0
 
LVL 12

Expert Comment

by:Ammar Gaffar
ID: 40511509
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
 

Accepted Solution

by:
Jason earned 0 total points
ID: 40523520
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
 

Author Closing Comment

by:Jason
ID: 40529997
assistance did not really help
thank for you efforts though
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question