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

Sorting in a Listview

My question is this:

How can I apply some kind of a function to a listview that allows it to sort
by both string and numbers whenever a header is clicked?

For example:

-------------------
INDEX
-------------------
1
-------------------
2
-------------------
3
------------------
hello
------------------
and
------------------

I want the user to be able to click the "INDEX" header and sort the
corresponding results by letters and numbers. I've searched the net for code,
but it either doesn't work or I can only click the header once and it will
not sort again.

Any suggestions?
Nov 17 '05 #1
6 1513
Doesn't the listview control have a property called "Sorting" that does all
of this for you?

"ReMEn" wrote:
My question is this:

How can I apply some kind of a function to a listview that allows it to sort
by both string and numbers whenever a header is clicked?

For example:

-------------------
INDEX
-------------------
1
-------------------
2
-------------------
3
------------------
hello
------------------
and
------------------

I want the user to be able to click the "INDEX" header and sort the
corresponding results by letters and numbers. I've searched the net for code,
but it either doesn't work or I can only click the header once and it will
not sort again.

Any suggestions?

Nov 17 '05 #2
It has a property called "sorting", and it gives you the options of ascending
and descending.

It's not very effective though. It only sorts the first column of items
according to what you specified. Also, you cannot click on the headers to
sort the other columns.

In a normal listview in various programs, you can just click on the header
and it will reverse the sorting each time you click it
(Ascending-descending). The sorting property doesn't allow this...
Nov 17 '05 #3
I chopped together some code for you. Download ListView.zip from

http://simsof.united.net.kg/samples/

This sample has one column of numbers and a second column of strings. Click
on a column to make it the sort column. Click on it again and it will toggle
between ascending and descending orders. I just threw it together quicklike
so if there is a bug or something doesn't seem to work let me know. You will
need to make adjustments to make it work with whatever code you want because
I had to hard code when it sorted numerically and when it sorted by strings.

mosimu

"ReMEn" wrote:
My question is this:

How can I apply some kind of a function to a listview that allows it to sort
by both string and numbers whenever a header is clicked?

For example:

-------------------
INDEX
-------------------
1
-------------------
2
-------------------
3
------------------
hello
------------------
and
------------------

I want the user to be able to click the "INDEX" header and sort the
corresponding results by letters and numbers. I've searched the net for code,
but it either doesn't work or I can only click the header once and it will
not sort again.

Any suggestions?

Nov 17 '05 #4
The code you provided works with no problems on columns with only integers or
only strings. However, I have a column with a mixed string containing both
letters and numbers. So when I click the header of the column containing the
mixed string, I get "Input string was not in correct format" error.

Any ideas around this?

Thanks
Nov 17 '05 #5
By the the way, after the debugging, it appears this line (& probably the
next line) are the problem lines:

int ione = one->SubItems->Item[col]->Text->ToInt32(0);

Hope that helps
Nov 17 '05 #6
ReMEn wrote:
The code you provided works with no problems on columns with only integers or
only strings. However, I have a column with a mixed string containing both
letters and numbers. So when I click the header of the column containing the
mixed string, I get "Input string was not in correct format" error.

Any ideas around this?

Thanks


You define a compare function

=== snippet from my codes: ===
int CALLBACK ListCtrl::compareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
...
}
==============================

and call it from function reacting to LVN_COLUMNCLICK notifcation
=== snippet from my codes: ===
void ListCtrl::sortCol(int iCol) // dispatch LVN_COLUMNCLICK, iCol is ((LPNMLISTVIEW)lParam)->iSubItem
{
int param3;
...

ListView_SortItems( hwndLV, (PFNLVCOMPARE) ListCtrl::compareFunc, (LPARAM)(param3));
...
}
==============================

And it's your compare Function which decide how to sort the data.

In you case, the user interface need to remember the last option of sorting,
and decide which way to do next, and encode this info in the 3rd parameter of
the ListView_SortItems(). Your compareFunc should decode the parameter and
do (or call separate functions of) comparaison of the data.

Because these things is enough complecated, I wrote in separate ListCtrl class once
and forgot the the details for forever, happy to be a client user of my own class. :)
muchan
Nov 17 '05 #7

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

Similar topics

0
by: rmorvay | last post by:
I have successfully integrated sorting in the listview control with the following code: Private Sub ListView_ColumnClick(ByVal sender As Object, ByVal e As...
0
by: Abhishek | last post by:
Hi! I am using listview control having 4 columns. I want it to behave in a similar manner as the details view of windows explorer. i.e it should sort on all the columns and all the columns...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
2
by: Li Pang | last post by:
Hi, I use a listview to populate the data, and define a sorting direction with Me.ListView1.Sorting = System.Windows.Forms.SortOrder.Ascending so far no problem. As long as I add a new row...
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...
2
by: MikeY | last post by:
Hi everyone, After reading various posts I'm still scratching my head and unsure of what approach to take. I have created buttons that upon clicking, the buttons add an item name (myName) to my...
2
by: jediknight | last post by:
Hi, I have a listview which has columns of text and columns of numerical data. I need to be able to sort these columns into ascending/desending order whenever the user clicks on the column...
2
by: Steve Z. | last post by:
I am trying to implement the sorting of a ListView object by clicking on a column (using the example at:...
3
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2 for C# windows application. One of my column on the Listview control has numericstring. Other columns are sorting correctly except this one. It seems to be sorting...
2
by: win | last post by:
The SortKey of VB6 is not in .Net. Can I still do sorting using a listview? Thanks
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: 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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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.