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

ListView sort

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
11 9963
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mike Barrett | last post by:
I have a strange one here and I believe it is a bug. I googled it and saw others having a similar issue and nobody seems to have a fix. I will take another shot. I get this ERROR MESSAGE... ...
1
by: neelu | last post by:
I have four columns in listview. SR Number Part# DES Qty Weight 59 9410106800 Nut 10 0.03 60 90304ge800 Helmet 5 0.325 61 9635439604 ...
0
by: Mike Barrett | last post by:
I have a strange one here and I believe it is a bug. I googled it and saw others having a similar issue and nobody seems to have a fix. I will take another shot. I get this ERROR MESSAGE... ...
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
7
by: BobAchgill | last post by:
I am trying to decide which of these controls to use to implement letting my user select a full row from MyList. The MyList has several columns which would be nice to sort by at run time. The...
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...
0
by: Terry Brown | last post by:
I have a form which contains a listview item. The form is created to view data that is generated by interaction with a separate form. There are buttons on the form that change the data source...
6
by: Dave | last post by:
VB6 has a SorkKey property that you can setup on the ListView control to tell the ListView what column to use for sorting. In .NET there is a Sort() method and a SortOrder property that you can...
4
by: forest demon | last post by:
I have an IList/Collection that contains items in a ListView. If i click on an item in the ListView, i can capture the index (lv.SelectedItems.Index) and reference the correct item in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...

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.