List View Sorting Question

A

> Adrian

In VS 2003 I could sort a column in a list view using the code below,
hover, in V2005 I get this error:
Error 1 Using the generic type 'System.Collections.Generic.IComparer<T>'
requires '1' type arguments

What to do?

Please tell me concretely how to get rid of this error in terms of code.

Many thanks,
Adrian

private void ColumnClick(object o, ColumnClickEventArgs e)
{
// Set the ListViewItemSorter property to a new
ListViewItemComparer object.
this.listView1.ListViewItemSorter = new
ListViewItemComparer(e.Column);
// Call the sort method to manually sort the column based on the
ListViewItemComparer implementation.
listView1.Sort();
}


// Implements the manual sorting of items by columns.
class ListViewItemComparer : IComparer ****error line***
{
private int col;
public ListViewItemComparer()
{
col = 0;
}
public ListViewItemComparer(int column)
{
col = column;
}
public int Compare(object x, object y)
{
return String.Compare(((ListViewItem)x).SubItems[col].Text,
((ListViewItem)y).SubItems[col].Text);
}
}
 
A

Ashot Geodakov

I see that you are trying to compare ListViewItems as Strings, so you need
to specify the string type as a generic type parameter:

class ListViewItemComparer : IComparer< string>

and you need to declare the Compare method with string as parameter type:

public int Compare(string x, string y)
 
A

> Adrian

Sorry people, this went the worng way.
Adrian.


> Adrian said:
The solution was simple after all.
Thank you guys.
Adrian
---
> Adrian said:
In VS 2003 I could sort a column in a list view using the code below,
hover, in V2005 I get this error:
Error 1 Using the generic type 'System.Collections.Generic.IComparer<T>'
requires '1' type arguments

What to do?

Please tell me concretely how to get rid of this error in terms of code.

Many thanks,
Adrian

private void ColumnClick(object o, ColumnClickEventArgs e)
{
// Set the ListViewItemSorter property to a new
ListViewItemComparer object.
this.listView1.ListViewItemSorter = new
ListViewItemComparer(e.Column);
// Call the sort method to manually sort the column based on the
ListViewItemComparer implementation.
listView1.Sort();
}


// Implements the manual sorting of items by columns.
class ListViewItemComparer : IComparer ****error line***
{
private int col;
public ListViewItemComparer()
{
col = 0;
}
public ListViewItemComparer(int column)
{
col = column;
}
public int Compare(object x, object y)
{
return String.Compare(((ListViewItem)x).SubItems[col].Text,
((ListViewItem)y).SubItems[col].Text);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top