473,662 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sort ListView By column

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 Cap 15 0.421
62 25340h1245 Shoes 2 0.001

I want that when I click on column part# it sort the list View
according to part# column. If I click on ColumnName DES it sort the
whole listView according to DES Column as so on. All rows must be
sorted according to the column I clicked. How can I do that.

Thanks.

Nov 17 '05 #1
1 15006
Hi Neelu,
you need to do a few things:

1. Add an event handler to the ListViews ColumnClick event
2. Inside this event handler you will need to set the Lists
ListViewItemSor ter property to supply an object that knows how to sort the
data (see example later)
3. Call the Lists Sort() method which will cause the listview to be reordered.
The ListViewItemSor ter object needs to implement IComparer so that the
control can ask it how to compare two items in the list, it will also need
to know the column index you clicked on to be able to retrieve the data from
the correct column. Depending on the column you will have to run a different
comparison algorithm (i.e. strings vs ints vs special formatting etc).

For example, pretend we have a form with a ListView called ListView1, it has
multiple columns, you need to add an even handler for the column click:

private void Form1_Load(obje ct sender, EventArgs e)
{
this.listView1. ColumnClick += new
ColumnClickEven tHandler(listVi ew1_ColumnClick );
}

void listView1_Colum nClick(object sender, ColumnClickEven tArgs e)
{
this.listView1. ListViewItemSor ter = new
MyListViewSorte r(e.Column);
this.listView1. Sort();
}
You need to define the MyListViewSorte r (or whatever you want to call it
class):
public class MyListViewSorte r : IComparer
{
private int _columnIndex;

public MyListViewSorte r(int columnIndex)
{
_columnIndex = columnIndex;
}

#region IComparer Members

public int Compare(object x, object y)
{
ListViewItem lvi1 = x as ListViewItem;
ListViewItem lvi2 = y as ListViewItem;

return
lvi1.SubItems[_columnIndex].Text.CompareTo (lvi2.SubItems[_columnIndex].Text);
}

#endregion
}
In your case inside the Compare method you will have to call different
functions to do the comparison depending on the index of the column the user
clicked on.

Hope that helps
Mark R Dawson
http://www.markdawson.org

"neelu" wrote:
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 Cap 15 0.421
62 25340h1245 Shoes 2 0.001

I want that when I click on column part# it sort the list View
according to part# column. If I click on ColumnName DES it sort the
whole listView according to DES Column as so on. All rows must be
sorted according to the column I clicked. How can I do that.

Thanks.

Nov 17 '05 #2

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

Similar topics

3
2965
by: Bob Dankert | last post by:
Is there any way to maintain the sort order of a sort on a 2D array? For example: I have the array: 1,a 2,a 3,f 4,a 5,s 6,a 7,z 8,b and sort it by the second column, and I get: 4,a 6,a 1,a 2,a 8,b 3,f 5,s 7,z
1
1884
by: jrhoads23 | last post by:
Hello, I subclassed my own ListView which supports column sorting. It automatically draws the up/down sort arrows in the column header. The arrows that are used are drawn by me. I noticed that Windows XP systems uses different sort arrows than Windows 2000 and earlier systems. (open Windows Explorer and check them out on the two differnt O/S) I would like my ListView to use the same arrows as the operating system uses. Is there an API...
3
5889
by: Steve | last post by:
I have windows form with ListView control. The ListView control has few columns which user can sort by clicking the column header. I want the column header have the small triangle indicator of sort order. How can I implement that? Thanks
4
18197
by: Robin Tucker | last post by:
How do I sort the items in a list box? I am using a class derived from IComparer to sort items on columns in a ListView, but the ListBox doesn't support this kind of facility. The "items" in my list box aren't just strings, they are a class I have defined for each list item. There is a comparitor function, but how can I write - ListBox.Items.Sort() in this instance? Do I need to do my own quicksort?
1
1256
by: Jean-Claude Bertrand | last post by:
Hi! Windows of the application IO have to built (my first one in vb.Net) is made from 2 part: a) A grid wich will be used only to display the main information of the current record (so the grid must select the entire row and not allow editing). I have selected the datagrid so far and succed after many research to allow only entire row selection with this help http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q792q
0
1042
by: Frank Rizzo | last post by:
I've implemented sort via the well known method of assigning the an IComparer class to the .ListViewItemSorter property. It works fine except for one thing. If I had rearranged the column order in the listview and then sort the column, the listview control returns the column order to its original state. How can I tell it not to? Thanks Frank
0
1778
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 that is displayed in the listview. The listview can be sorted by clicking on columns in the listview. When I change the data for the listview I use listview1.Clear() to clear the listview, then rebuild the listview by adding the proper columns...
11
9985
by: Alan T | last post by:
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 ?
13
5940
by: Vbbeginner07 | last post by:
its about Sorting a list view but its not working. please help....... Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader) 'Determine whether the column is the same as the last column clicked. If e.Column <> sortColumn Then 'Set the sort column to the new column. sortColumn = e.Column 'Set the sort order to ascending by default. ListView1.Sorted = SortOrder.Ascending
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8547
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4181
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1754
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.