473,396 Members | 2,004 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,396 software developers and data experts.

Bug with listview using custom ListViewSorter

I am trying to create a custom listviewsorter by implementing IComparer. My
listview has check boxes and the purpose is to move checked item to top of
list. They all will be sorted ascendingly. It throws error "-1 is not a
valid index" when the sorting is done with listview. I have no problem using
the same custom sorter for an arraylist of listviewitems. A bug in listview
control? Any suggestion?

Thanks,
-sorpor

Here is the code for the comparer:

internal class listviewsorter:IComparer

{

#region IComparer Members

public int Compare(object x, object y)

{

int result = 0;

ListViewItem itmX = (ListViewItem)x;

ListViewItem itmY = (ListViewItem)y;
if (itmX.Checked == itmY.Checked)

result = String.Compare(itmX.Text, itmY.Text);

else

{

System.Diagnostics.Trace.WriteLine("Bool");

result = itmY.Checked.CompareTo(itmX.Checked); //checked < not checked

}

return result;

}

#endregion

}

Works fine with ArrayList sorting:

ArrayList testItms = new ArrayList();

ListViewItem a = new ListViewItem("A");

ListViewItem b = new ListViewItem("B");

ListViewItem c = new ListViewItem("C");

c.Checked = true;

testItms.Add(b);

testItms.Add(a);

testItms.Add(c);

foreach(ListViewItem itm in testItms) //b a c

{

System.Diagnostics.Trace.WriteLine(itm.Text);

}

testItms.Sort(new listviewsorter());

foreach(ListViewItem itm in testItms) //c a b

{

System.Diagnostics.Trace.WriteLine(itm.Text);

}
Nov 17 '05 #1
0 1628

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

Similar topics

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....
1
by: newbie | last post by:
Hello, I maybe asking too much in a single posting, but here it goes: I building a windows form that mimic's the Outlook XP GUI. Its a three pane form that will allow a user to view and edit...
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...
1
by: redneon | last post by:
Is it possible to get the Handle property for a particular cell in a ListView? I'm wanting to turn a cell into a progress bar rather than just displaying text. If it's possible to get the Handle I...
5
by: ToddH | last post by:
I know the following code is C#. I'm a vb programmer trying to learn a new language. I posted this in the c# group but never got a response. You guys seem to know alot about all languages and have...
5
by: David Gouge | last post by:
Hi all, Hope this is a simple one for you but i seem to be having a bit of a mental block. I have an inherited ListView (NewListView) class and an inherited ListViewItem (NewListViewItem)...
3
by: Michael.Suarez | last post by:
Is it me, or does it seem like they put no effort into creating the listview control in .Net. listview. A few gripes I have with .Net listview that aren't present in vb6: -Inability to set...
7
by: =?Utf-8?B?YnJhaW5mdWVsbWVkaWE=?= | last post by:
Can anyone point me in the direction of creating a custom listview item? I guess the other question then - is this possible? I want to create a list of listview items comprised each comprised of...
3
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2 for C# windows application. One of my column on the Listview control has numericstring. Other columns are sorting correctly except this one. It seems to be sorting...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...
0
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...
0
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,...

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.