Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to sort a TListView on a column header click Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
31-Oct-02
Category
VCL-General
Language
Delphi 2.x
Views
166
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

How to sort a TListView on a column header click

Answer:

In the object inspector, I have set the ListView's SortType = stText.

1   {Private Declarations}
2   SortColumn: Integer;
3   SortDescending: Boolean;
4   
5   procedure Sort(Column: Integer);
6   
7   procedure TfrmMain.ListViewColumnClick(Sender: TObject; Column: TListColumn);
8   begin
9     Sort(Column.Index);
10  end;
11  
12  procedure TfrmMain.Sort(Column: Integer);
13  begin
14    if SortColumn = Column then
15      SortDescending := not SortDescending
16    else
17    begin
18      SortDescending := False;
19      SortColumn := Column;
20    end;
21    lsvPlayerPages.AlphaSort;
22  end;
23  
24  procedure TfrmMain.ListViewCompare(Sender: TObject; Item1, Item2: TListItem;
25    Data: Integer; var Compare: Integer);
26  begin
27    if SortColumn = 0 then
28      Compare := CompareStr(Item1.Caption, Item2.Caption)
29    else
30      Compare := CompareStr(Item1.SubItems[Pred(SortColumn)],
31        Item2.SubItems[Pred(SortColumn)]);
32    if SortDescending then
33      Compare := -Compare;
34  end;


I have made a separate sort procedure to be able to sort from different commands (e.g. from a menu). The code also sorts descending if a column header is clicked a second time (as Windows Explorer does).

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC