473,473 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

BUG REPORT: ListView Control

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


Jul 21 '05 #1
0 1339

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

Similar topics

2
by: Geert-Pieter Hof | last post by:
Hello, I use a ListView control (report view) to show the user a summary of error messages. The width of the column that holds the error messages is such that no horizontal scrollbar is needed....
3
by: pointBoarder | last post by:
I'm following the article from http://support.microsoft.com/default.aspx?scid=kb;en-us;155178 The above article explains how to fill a listview. For some reason I keep recieving "User-defined...
1
by: Welie | last post by:
Hi all- I am using a listview (MSComctlLib.ListViewCtrl.2)on an Access form. Actually there are six listviews on the form. I need to do the same thing to all six forms so I have the loop below....
10
by: Vinay | last post by:
Hi, Is there a limit on maximum number of columns in listview control ? I am finding: In a listview control (Report Format) I am displaying a record with 400 fields. I am creating its column...
12
by: Dennis | last post by:
I have a form which has a ListView control named ListView1 added at design time. When I add items using the following code, they don't appear in the list view. However, if I create a ListView...
3
by: Nick | last post by:
Hi there, I'm hunting around for some kind of example on how to customize the largeicons view of a listview control, I would like to draw each item myself if possible, is it possible to owner...
6
by: Jack | last post by:
Hello, I've noticed through searching this group's previous posts that one can get the item the mouse is over in a listview control but I did not see how to get the subitem the mouse is over. Is...
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... ...
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: 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
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.