473,704 Members | 2,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons.IComparer.C ompare
Dim lv1 As ListViewItem = x
Dim lv2 As ListViewItem = y
If mNumeric Then
If m_bAltSort = True Then
Return CLng(lv1.SubIte ms(mColumn).Tex t) +
CDbl(lv2.SubIte ms(mColumn).Tex t)
Else
Return CDbl(lv1.SubIte ms(mColumn).Tex t) -
CDbl(lv2.SubIte ms(mColumn).Tex t)
End If
Else
If m_bAltSort = True Then
Return String.Compare( lv2.SubItems(mC olumn).Text,
lv1.SubItems(mC olumn).Text)
Else
Return String.Compare( lv1.SubItems(mC olumn).Text,
lv2.SubItems(mC olumn).Text)
End If
End If
End Function

End Class

CLASS FOUND ON MSFT KB:

Imports System.Collecti ons
Imports System.Windows. Forms

Public Class ListViewColumnS orter
Implements System.Collecti ons.IComparer

Private ColumnToSort As Integer
Private OrderOfSort As SortOrder
Private ObjectCompare As CaseInsensitive Comparer

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 CaseInsensitive Comparer object.
ObjectCompare = New CaseInsensitive Comparer
End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer

Implements IComparer.Compa re
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.C ompare(listview X.SubItems(Colu mnToSort).Text,
listviewY.SubIt ems(ColumnToSor t).Text)

' Calculate the correct return value based on the object
' comparison.
If (OrderOfSort = SortOrder.Ascen ding) Then
' Ascending sort is selected, return typical result of
' compare operation.
Return compareResult
ElseIf (OrderOfSort = SortOrder.Desce nding) 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(B yVal lsv As ListView, ByVal rdr As
SqlClient.SqlDa taReader) 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).ToS tring
Dim lvi As New ListViewItem(id )
lsv.Items.Add(l vi)

For intCol = 1 To (rdr.FieldCount - 1)
lvi.SubItems.Ad d(rdr.Item(intC ol).ToString)
Next

End While

' refresh list
lsv.EndUpdate()
Catch ex As Exception
bRetVal = False
MsgBox(ex.Messa ge, MsgBoxStyle.Cri tical)
Finally
Fill_ListView = bRetVal
End Try
End Function


Nov 20 '05 #1
0 1442

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

Similar topics

0
1361
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... "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.
7
6448
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 ListView.Items.Add(), . I see that it is a virtual method so it should be easy to do. If anyone can help I would appreciate it greatly. I can do what I need to do in a different way this would just make everything significantly cleaner and eaasier...
3
9303
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, checkbox's and icons are displayed as they are meant to be. My problem is this, I am trying to achieve the same results as I get with CheckListBox(s), where I am able to select (Check-mark) various checkboxs items and able to alter the data. I do...
0
5420
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 board. I have a form that contains a ListView and a GroupBox control. The GroupBox control itself contains several TextBox Controls. I have Validating and Validated event handlers set for the GroupBox control, and a SelectedIndexChanged event...
4
2237
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 imagelist. but i notice that while i execute the project in .net, the step that sets the icon prop. throws an exception.
6
3411
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 listview, removing the item obviously keeps everything fine but the second I use insert the item is added to the *end* of the listview. Does anyone have any tips on how I can solve this issue?? Call Items.Remove(SourceItem)
2
3677
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
5063
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 application monitors all the textboxes and other input & display controls on that application. The data from the textboxes and listboxes are retrived fine. But, when it comes to Listview there I got memory exception. I know that I need to do the in memory...
4
4509
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 is that when clicking on the "Show details" button the ListView for the appropriate row binds in the codebehind and displays the detail data for the selected row. I did something similar with a gridview control previously, but want to be able to...
0
8684
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9134
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9025
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8975
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7887
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5930
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4699
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2478
muto222
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.