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

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 14988
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
ListViewItemSorter 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 ListViewItemSorter 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(object sender, EventArgs e)
{
this.listView1.ColumnClick += new
ColumnClickEventHandler(listView1_ColumnClick);
}

void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
this.listView1.ListViewItemSorter = new
MyListViewSorter(e.Column);
this.listView1.Sort();
}
You need to define the MyListViewSorter (or whatever you want to call it
class):
public class MyListViewSorter : IComparer
{
private int _columnIndex;

public MyListViewSorter(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
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...
1
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...
3
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...
4
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...
1
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...
0
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...
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...
11
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...
13
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.