By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,770 Members | 1,833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,770 IT Pros & Developers. It's quick & easy.

ListView sort

P: n/a
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 ?
Jul 11 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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.



"Alan T" <al*************@yahoo.com.auwrote in message
news:e4**************@TK2MSFTNGP03.phx.gbl...
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 ?


Jul 11 '06 #2

P: n/a
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

"TheSteph" <Th******@NoSpam.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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.



"Alan T" <al*************@yahoo.com.auwrote in message
news:e4**************@TK2MSFTNGP03.phx.gbl...
>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 ?



Jul 11 '06 #3

P: n/a
Is there a method to sort each column ?

"Claes Bergefall" <lo*****@nospam.nospamwrote in message
news:OA**************@TK2MSFTNGP04.phx.gbl...
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

"TheSteph" <Th******@NoSpam.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>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.



"Alan T" <al*************@yahoo.com.auwrote in message
news:e4**************@TK2MSFTNGP03.phx.gbl...
>>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 ?




Aug 10 '06 #4

P: n/a
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

"Alan T" <al*************@yahoo.com.auwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
Is there a method to sort each column ?

"Claes Bergefall" <lo*****@nospam.nospamwrote in message
news:OA**************@TK2MSFTNGP04.phx.gbl...
>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

"TheSteph" <Th******@NoSpam.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>>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.



"Alan T" <al*************@yahoo.com.auwrote in message
news:e4**************@TK2MSFTNGP03.phx.gbl...
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 ?




Aug 10 '06 #5

P: n/a
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" <lo*****@nospam.nospamwrote in message
news:Oc**************@TK2MSFTNGP04.phx.gbl...
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

"Alan T" <al*************@yahoo.com.auwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
>Is there a method to sort each column ?

"Claes Bergefall" <lo*****@nospam.nospamwrote in message
news:OA**************@TK2MSFTNGP04.phx.gbl...
>>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

"TheSteph" <Th******@NoSpam.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl.. .
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.



"Alan T" <al*************@yahoo.com.auwrote in message
news:e4**************@TK2MSFTNGP03.phx.gbl...
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 ?
>
>




Aug 13 '06 #6

P: n/a
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" <al*************@yahoo.com.auwrote in message
news:Od*************@TK2MSFTNGP05.phx.gbl...
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" <lo*****@nospam.nospamwrote in message
news:Oc**************@TK2MSFTNGP04.phx.gbl...
>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

"Alan T" <al*************@yahoo.com.auwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl. ..
>>Is there a method to sort each column ?

"Claes Bergefall" <lo*****@nospam.nospamwrote in message
news:OA**************@TK2MSFTNGP04.phx.gbl...
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

"TheSteph" <Th******@NoSpam.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl. ..
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.
>
>
>
>
>
>
>
"Alan T" <al*************@yahoo.com.auwrote in message
news:e4**************@TK2MSFTNGP03.phx.gbl.. .
>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 ?
>>
>>
>
>




Aug 13 '06 #7

P: n/a
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" <al*************@yahoo.com.auwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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" <al*************@yahoo.com.auwrote in message
news:Od*************@TK2MSFTNGP05.phx.gbl...
>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" <lo*****@nospam.nospamwrote in message
news:Oc**************@TK2MSFTNGP04.phx.gbl...
>>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

"Alan T" <al*************@yahoo.com.auwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl.. .
Is there a method to sort each column ?

"Claes Bergefall" <lo*****@nospam.nospamwrote in message
news:OA**************@TK2MSFTNGP04.phx.gbl...
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
>
"TheSteph" <Th******@NoSpam.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl.. .
>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.
>>
>>
>>
>>
>>
>>
>>
>"Alan T" <al*************@yahoo.com.auwrote in message
>news:e4**************@TK2MSFTNGP03.phx.gbl. ..
>>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 ?
>>>
>>>
>>
>>
>
>




Aug 14 '06 #8

P: n/a
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" <al*************@yahoo.com.auwrote in message
news:O6*************@TK2MSFTNGP06.phx.gbl...
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" <al*************@yahoo.com.auwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>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" <al*************@yahoo.com.auwrote in message
news:Od*************@TK2MSFTNGP05.phx.gbl...
>>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" <lo*****@nospam.nospamwrote in message
news:Oc**************@TK2MSFTNGP04.phx.gbl...
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

"Alan T" <al*************@yahoo.com.auwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl. ..
Is there a method to sort each column ?
>
"Claes Bergefall" <lo*****@nospam.nospamwrote in message
news:OA**************@TK2MSFTNGP04.phx.gbl.. .
>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
>>
>"TheSteph" <Th******@NoSpam.comwrote in message
>news:%2****************@TK2MSFTNGP05.phx.gbl. ..
>>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.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>"Alan T" <al*************@yahoo.com.auwrote in message
>>news:e4**************@TK2MSFTNGP03.phx.gbl.. .
>>>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 ?
>>>>
>>>>
>>>
>>>
>>
>>
>
>




Aug 14 '06 #9

P: n/a
There is a fairly good entry at MSDN that goes over this.

http://msdn.microsoft.com/library/de.../en-us/dnwinfo
rms/html/listviewsort.asp

*** Sent via Developersdex http://www.developersdex.com ***
Aug 30 '06 #10

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Sep 13 '06 #11

P: n/a
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

"Miguel Lachance" <mi******@hotmail.comwrote in message
news:ut**************@TK2MSFTNGP03.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***

Sep 13 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.