473,413 Members | 1,755 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,413 software developers and data experts.

Sorting ListView columns in .NET 2.0

I'm porting code from VS 2003 to VS 2005, and can't get column sorting
to work as before. This is code I found somewhere and used without
acctually understanding it. I still don't fully understand it, which
makes it hard to see what's wrong now. In VS 2003, it allowed me to
click on a ListView column to get the list contents sorted on that column.

private void listView1_ColumnClick(object sender,
ColumnClickEventArgs e)
{
this.listView1.ListViewItemSorter =
new ListViewItemComparer(e.Column);
}

class ListViewItemComparer : IComparer<object>
{
private int col;

public ListViewItemComparer()
{
col = 0;
}

public ListViewItemComparer(int col)
{
this.col = col;
}

public int Compare(object x, object y)
{
return String.Compare(((ListViewItem)x).SubItems[col].Text,
((ListViewItem)y).SubItems[col].Text);
}
}

What happens now is that I get an error here:

this.listView1.ListViewItemSorter =
new ListViewItemComparer(e.Column);

that says:

Cannot implicitly convert type 'MyNamespace.ListViewItemComparer' to
'System.Collections.IComparer'. An explicit conversion exists (are you
missing a cast?)

On this line:

class ListViewItemComparer : IComparer<object>

I added the "<object>" part to get VS to accept it. As can be seen, I'm
clueless about things ending with <>, and for now, I'm quite happy with
the cut-and-paste programming. Just hope someone can spot the error.

Gustaf
Oct 30 '06 #1
2 5977
Bob
Hi Gustaf,
This compiles, whether it sorts or not haven't tried.
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)

{

this.listView1.ListViewItemSorter =

new ListViewItemComparer(e.Column);
}

class ListViewItemComparer : IComparer

{

private int col;

public ListViewItemComparer()

{

col = 0;

}

public ListViewItemComparer(int col)

{

this.col = col;

}

public int Compare(object x, object y)

{

return String.Compare(((ListViewItem)x).SubItems[col].Text,

((ListViewItem)y).SubItems[col].Text);

}

}

hth

Bob

"Gustaf" <gu*****@algonet.sewrote in message
news:Ox****************@TK2MSFTNGP05.phx.gbl...
I'm porting code from VS 2003 to VS 2005, and can't get column sorting
to work as before. This is code I found somewhere and used without
acctually understanding it. I still don't fully understand it, which
makes it hard to see what's wrong now. In VS 2003, it allowed me to
click on a ListView column to get the list contents sorted on that column.

private void listView1_ColumnClick(object sender,
ColumnClickEventArgs e)
{
this.listView1.ListViewItemSorter =
new ListViewItemComparer(e.Column);
}

class ListViewItemComparer : IComparer<object>
{
private int col;

public ListViewItemComparer()
{
col = 0;
}

public ListViewItemComparer(int col)
{
this.col = col;
}

public int Compare(object x, object y)
{
return String.Compare(((ListViewItem)x).SubItems[col].Text,
((ListViewItem)y).SubItems[col].Text);
}
}

What happens now is that I get an error here:

this.listView1.ListViewItemSorter =
new ListViewItemComparer(e.Column);

that says:

Cannot implicitly convert type 'MyNamespace.ListViewItemComparer' to
'System.Collections.IComparer'. An explicit conversion exists (are you
missing a cast?)

On this line:

class ListViewItemComparer : IComparer<object>

I added the "<object>" part to get VS to accept it. As can be seen, I'm
clueless about things ending with <>, and for now, I'm quite happy with
the cut-and-paste programming. Just hope someone can spot the error.

Gustaf

Oct 30 '06 #2
Bob wrote:
This compiles, whether it sorts or not haven't tried.
Many thanks, Bob. I found the help in this article too:

http://support.microsoft.com/kb/319401/EN-US/

Gustaf
Nov 1 '06 #3

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

Similar topics

0
by: rmorvay | last post by:
I have successfully integrated sorting in the listview control with the following code: Private Sub ListView_ColumnClick(ByVal sender As Object, ByVal e As...
0
by: Abhishek | last post by:
Hi! I am using listview control having 4 columns. I want it to behave in a similar manner as the details view of windows explorer. i.e it should sort on all the columns and all the columns...
6
by: ReMEn | last post by:
My question is this: How can I apply some kind of a function to a listview that allows it to sort by both string and numbers whenever a header is clicked? For example: -------------------...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
3
by: Sudheer Kareem | last post by:
Hello All, Can anybody tell me the how to sort a list view in vb.net,I have used a list view in my project and the columns of the list view is dynamically created.if i click on the header of any...
5
by: billuy | last post by:
Is there a way to sort by a specified column in a listview? I'd like my users to be able to click on the column heading and have the listview sort by that column. Thanks, Billyb
3
by: Curtis | last post by:
I am trying to do a simple sort on a list view.With the listview control the first time I sort on the control it sorts the items but anytime after that it fails to sort. I'm sure this is a simple...
2
by: yxq | last post by:
Hello, By default, when set the listview's property "Sorting=Descending", the listview will sort the items base on the string in first column(column(0)) while adding items, how to specify listview...
2
by: jediknight | last post by:
Hi, I have a listview which has columns of text and columns of numerical data. I need to be able to sort these columns into ascending/desending order whenever the user clicks on the column...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
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...

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.