Solved

listview sort error

Posted on 2014-12-20
5
134 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Handling arrays 14 33
Hide Tab Page 3 18
Get String split 5 33
Showdialog 8 20
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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now