ListView error in Winform - vb.net

M

Mike Barrett

I have a strange one here and I believe it is a bug. I googled it and saw
others having a similar issue and nobody seems to have a fix. I will take
another shot. I also posted to the control group, but this problem is a bit
rare so I figured I'd broaden my horizons in the hopes someone else has
encountered it.

-------------------------------------------------

I get this ERROR MESSAGE...

"Specified argument was out of the range of valid values. Parameter Name:
'9' is not a valid value for 'index'."

....when I attempt to "Refresh" a ListView control AFTER it has been sorted.
I have tried by Sort classes below because, inexplicably, MSFT stripped the
column-click Sort capabilities from the .Net ListView control. If I do not
sort the ListView and refresh it, it works fine.

I am populating the ListView from a DataReader and it works great. The
function is used over and over and it only fails after a sort. Basically,
the users select a few rows in the list, some SQL executes using their
values, and the ListView refreshes via the function I wrote. It works.

The strangest part of this error is that it adds the first item to teh list
successfully every time, but fails when adding the second item which was
successfully created. I have been playing with this all day. It appears to
be a Bug. If anyone has overcome this issue, please let me know. All Sort
class code is below.

Thank you!

Mike Barrett


----------------------------------------------------------------------------
-

CLASS CODE:

CLASS PROVIDED BY DEVELOPER IN THIS FORUM:

Public Class CSortClass
Implements IComparer
Private mColumn As Integer
Private mNumeric As Boolean
Private m_bAltSort As Boolean

Public Property column() As Integer
Get
Return mColumn
End Get
Set(ByVal Value As Integer)
mColumn = Value
End Set
End Property
Public Property Numeric() As Boolean
Get
Return mNumeric
End Get
Set(ByVal Value As Boolean)
mNumeric = Value
End Set
End Property
Public Property AltSort() As Boolean
Get
Return m_bAltSort
End Get
Set(ByVal Value As Boolean)
m_bAltSort = Value
End Set
End Property

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements System.Collections.IComparer.Compare
Dim lv1 As ListViewItem = x
Dim lv2 As ListViewItem = y
If mNumeric Then
If m_bAltSort = True Then
Return CLng(lv1.SubItems(mColumn).Text) +
CDbl(lv2.SubItems(mColumn).Text)
Else
Return CDbl(lv1.SubItems(mColumn).Text) -
CDbl(lv2.SubItems(mColumn).Text)
End If
Else
If m_bAltSort = True Then
Return String.Compare(lv2.SubItems(mColumn).Text,
lv1.SubItems(mColumn).Text)
Else
Return String.Compare(lv1.SubItems(mColumn).Text,
lv2.SubItems(mColumn).Text)
End If
End If
End Function

End Class

CLASS FOUND ON MSFT KB:

Imports System.Collections
Imports System.Windows.Forms

Public Class ListViewColumnSorter
Implements System.Collections.IComparer

Private ColumnToSort As Integer
Private OrderOfSort As SortOrder
Private ObjectCompare As CaseInsensitiveComparer

Public Property SortColumn() As Integer
Set(ByVal Value As Integer)
ColumnToSort = Value
End Set

Get
Return ColumnToSort
End Get
End Property

Public Property Order() As SortOrder
Set(ByVal Value As SortOrder)
OrderOfSort = Value
End Set

Get
Return OrderOfSort
End Get
End Property

Public Sub New()
' Initialize the column to '0'.
ColumnToSort = 0

' Initialize the sort order to 'none'.
OrderOfSort = SortOrder.None

' Initialize the CaseInsensitiveComparer object.
ObjectCompare = New CaseInsensitiveComparer
End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compare
Dim compareResult As Integer
Dim listviewX As ListViewItem
Dim listviewY As ListViewItem

' Cast the objects to be compared to ListViewItem objects.
listviewX = CType(x, ListViewItem)
listviewY = CType(y, ListViewItem)

' Compare the two items.
compareResult =
ObjectCompare.Compare(listviewX.SubItems(ColumnToSort).Text,
listviewY.SubItems(ColumnToSort).Text)

' Calculate the correct return value based on the object
' comparison.
If (OrderOfSort = SortOrder.Ascending) Then
' Ascending sort is selected, return typical result of
' compare operation.
Return compareResult
ElseIf (OrderOfSort = SortOrder.Descending) Then
' Descending sort is selected, return negative result of
' compare operation.
Return (-compareResult)
Else
' Return '0' to indicate that they are equal.
Return 0
End If
End Function
End Class

MY FUNCTION:

Function Fill_ListView(ByVal lsv As ListView, ByVal rdr As
SqlClient.SqlDataReader) As Boolean
Dim bRetVal As Boolean = True
Dim intLoop As Integer
Dim id As String
Dim intCol As Integer

Try
' prepare for update
lsv.BeginUpdate()

' clear list
lsv.Items.Clear()

While rdr.Read
id = rdr.Item(0).ToString
Dim lvi As New ListViewItem(id)
lsv.Items.Add(lvi)

For intCol = 1 To (rdr.FieldCount - 1)
lvi.SubItems.Add(rdr.Item(intCol).ToString)
Next

End While

' refresh list
lsv.EndUpdate()
Catch ex As Exception
bRetVal = False
MsgBox(ex.Message, MsgBoxStyle.Critical)
Finally
Fill_ListView = bRetVal
End Try
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top