ListView sort

A

Alan T

Does the ListView supports sort?
I want to have a up/down arrow/triangle that show it is sorted asc or desc
on the column headers when I click the column header.
May be I need a third-party components ?
 
T

TheSteph

With .NET 2.0 you can do all of that.



- You can assign an icon to each ColumnHeader of the ListView. You
just need to add an "ArrowUp", "ArrowDown" and one "SpacerIcon" (a fully
transparent Icon) to the Imagelist of the ListView, and select the right
ImageIndex according to your sorting order and the column on witch data are
sorted.



- You can make your own "ListViewItemSorter" (object that implement
ICompare) and assign it to ListView.ListViewItemSorter to sort the ListView
the way you want.



Steph.
 
C

Claes Bergefall

No need for a "SpacerIcon". Just set the image index to -1 to remove the
image
Btw, this technique can be used in 1.1 as well. Only difference is that
you'll need P/Invoke to set the image index and to assign the image list to
the header.

/claes
 
A

Alan T

Is there a method to sort each column ?

Claes Bergefall said:
No need for a "SpacerIcon". Just set the image index to -1 to remove the
image
Btw, this technique can be used in 1.1 as well. Only difference is that
you'll need P/Invoke to set the image index and to assign the image list
to the header.

/claes
 
C

Claes Bergefall

There's no automatic sorting if that's what you're asking for. You'll need
to implement IComparer and then assign an instance of that to the
ListViewItemSorter property

/claes
 
A

Alan T

Hi,

This is an example I found from Help:

class ListViewItemComparer : IComparer
{
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);
}
}


If I put this as a separate .cs file, what should I put in the using list ?
eg.
using xxxxx?
 
A

Alan T

In addition,

I got a compilation error:

Using the generic type 'System.Collections.Generic.IComparer<T>' requires
'1' type arguments.

What is that means?


Alan T said:
Hi,

This is an example I found from Help:

class ListViewItemComparer : IComparer
{
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);
}
}


If I put this as a separate .cs file, what should I put in the using list
?
eg.
using xxxxx?



Claes Bergefall said:
There's no automatic sorting if that's what you're asking for. You'll
need to implement IComparer and then assign an instance of that to the
ListViewItemSorter property

/claes
 
A

Alan T

Hi,

For temporary solution I put the class in the same unit of the listview and
it is ok in compilation.

However, I can only make it work in ascending order.
What I want is it can also sort in descending order if it is already in
ascending order.
Can I do that?

Alan T said:
In addition,

I got a compilation error:

Using the generic type 'System.Collections.Generic.IComparer<T>' requires
'1' type arguments.

What is that means?


Alan T said:
Hi,

This is an example I found from Help:

class ListViewItemComparer : IComparer
{
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);
}
}


If I put this as a separate .cs file, what should I put in the using list
?
eg.
using xxxxx?



Claes Bergefall said:
There's no automatic sorting if that's what you're asking for. You'll
need to implement IComparer and then assign an instance of that to the
ListViewItemSorter property

/claes

Is there a method to sort each column ?

No need for a "SpacerIcon". Just set the image index to -1 to remove
the image
Btw, this technique can be used in 1.1 as well. Only difference is
that you'll need P/Invoke to set the image index and to assign the
image list to the header.

/claes

With .NET 2.0 you can do all of that.



- You can assign an icon to each ColumnHeader of the
ListView. You
just need to add an "ArrowUp", "ArrowDown" and one "SpacerIcon" (a
fully
transparent Icon) to the Imagelist of the ListView, and select the
right
ImageIndex according to your sorting order and the column on witch
data are
sorted.



- You can make your own "ListViewItemSorter" (object that
implement
ICompare) and assign it to ListView.ListViewItemSorter to sort the
ListView
the way you want.



Steph.







Does the ListView supports sort?
I want to have a up/down arrow/triangle that show it is sorted asc
or desc
on the column headers when I click the column header.
May be I need a third-party components ?
 
C

Claes Bergefall

Just keep track of which sort order you currently have and then reverse your
compare statement (i.e. do String.Compare(y,x) for descending order). As for
the compilation error I don't see what that comes from, since the code you
posted doesn't use generics.

You can put the class in a separate file if you want. You don't need a using
statement for that since it will probably be in the same namespace anyway.
Personaly I put these kind of classes as internal inside the class that uses
it.

/claes


Alan T said:
Hi,

For temporary solution I put the class in the same unit of the listview
and it is ok in compilation.

However, I can only make it work in ascending order.
What I want is it can also sort in descending order if it is already in
ascending order.
Can I do that?

Alan T said:
In addition,

I got a compilation error:

Using the generic type 'System.Collections.Generic.IComparer<T>' requires
'1' type arguments.

What is that means?


Alan T said:
Hi,

This is an example I found from Help:

class ListViewItemComparer : IComparer
{
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);
}
}


If I put this as a separate .cs file, what should I put in the using
list ?
eg.
using xxxxx?



There's no automatic sorting if that's what you're asking for. You'll
need to implement IComparer and then assign an instance of that to the
ListViewItemSorter property

/claes

Is there a method to sort each column ?

No need for a "SpacerIcon". Just set the image index to -1 to remove
the image
Btw, this technique can be used in 1.1 as well. Only difference is
that you'll need P/Invoke to set the image index and to assign the
image list to the header.

/claes

With .NET 2.0 you can do all of that.



- You can assign an icon to each ColumnHeader of the
ListView. You
just need to add an "ArrowUp", "ArrowDown" and one "SpacerIcon" (a
fully
transparent Icon) to the Imagelist of the ListView, and select the
right
ImageIndex according to your sorting order and the column on witch
data are
sorted.



- You can make your own "ListViewItemSorter" (object that
implement
ICompare) and assign it to ListView.ListViewItemSorter to sort the
ListView
the way you want.



Steph.







Does the ListView supports sort?
I want to have a up/down arrow/triangle that show it is sorted asc
or desc
on the column headers when I click the column header.
May be I need a third-party components ?
 
M

Miguel Lachance

Hi I have almost finished an application in VB.NET.
I already use a listview to display all my stuff and i'm able to sort it
but i just can't display the arrows showing the sort order. Do you have
an example that i could use to apply your technique?

Thanks
Mig
 
C

Claes Bergefall

I have no idea what (or who) you're replying to here. And you're also in the
wrong NG if this is regarding VB.NET

To answer your question. Basically you assign two images (one up arrow and
one down arrow) to the image list and then set the image index for the
column where you want it to show (and imageindex for all other columns
to -1). See ColumnHeader.ImageIndex

/claes
 

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