473,394 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

ListView issue

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 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 the 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(ColumnToS ort).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


Nov 20 '05 #1
0 1425

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mike Barrett | last post by:
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 get this ERROR MESSAGE... ...
7
by: Dave Y | last post by:
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override...
3
by: MikeY | last post by:
Hopefully someone can help me on this. I am using C#, making Windows forms. I have created a listView with checkbox's. I have enabled the checkboxes under the properties, and all the data,...
0
by: PeacError | last post by:
Using Microsoft Visual Studio .NET 2003, Visual C# .NET 1.1: I apologize if this question has been addressed elsewhere, but I could not find a reference to it in the search engine for this...
4
by: Ira Siyal | last post by:
Hi I had a project in VB6 which I upgraded to VB.NET recently. In the app, I am facing some issue with listview control. In the earlier app, I have set the Icons in listview through an...
6
by: Nick | last post by:
Hi there, I'm trying to implement drag-drop for my listview control in large icon view mode. Unfortunately the order of the items gets completely messed up upon inserting the item back into the...
2
by: Dave | last post by:
When converting VB6 ListView code to DotNet, what do you do to replace the ListView.FocusedItem object, specifically I want to reference ListView.FocusedItem.Top Thanks Dave
2
by: =?Utf-8?B?SXJmYW4=?= | last post by:
Hello, It may be a repeated question but I don't find the solution to the situation that I encounter in it. My application is monitoring another application that is built in VB6. The...
4
by: Brian Gaze | last post by:
I have created a ListView control and have bound this to a datasource. Within the ItemTemplate of the ListView I have added another ListViewControl which is databound in the code behind. The idea...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.