Sorting a ListView
Here is how you can use the OnColumnClick and OnCompare events of a TListView to let users sort the columns (ascending/descending) in a report-style list view by clicking on the column headers. This requires a global variable to keep track of the column that was clicked:
-
int ColumnToSort; // add this global var in the private section of your TForm1 Class....
-
-
//---------------------------------------------------------------------------
-
-
void __fastcall TForm1::ListView1ColumnClick(TObject *Sender,
-
TListColumn *Column)
-
{
-
-
ColumnToSort = Column->Index;
-
ListView1->AlphaSort(); //trigger ListView1Compare
-
-
if (ColumnToSort==0)
-
ListView1->Tag=(ListView1->Tag==1) ? 0:1 ;
-
else
-
ListView1->Columns->Items[ColumnToSort-1]->Tag=(ListView1->Columns->Items[ColumnToSort-1]->Tag==1) ? 0:1;
-
-
}
-
//---------------------------------------------------------------------------
-
-
void __fastcall TForm1::ListView1Compare(TObject *Sender, TListItem *Item1,
-
TListItem *Item2, int Data, int &Compare)
-
{
-
AnsiString Str1,Str2;
-
int ColTag;
-
-
if (ColumnToSort == 0)
-
{
-
ColTag=ListView1->Tag;
-
Str1=Item1->Caption;
-
Str2=Item2->Caption;
-
}
-
else
-
{
-
ColTag=ListView1->Columns->Items[ColumnToSort-1]->Tag;
-
Str1=Item1->SubItems->Strings[ColumnToSort-1];
-
Str2=Item2->SubItems->Strings[ColumnToSort-1];
-
}
-
-
if (ColTag==1)
-
Compare=CompareText(Str1,Str2);
-
else
-
Compare=CompareText(Str2,Str1);
-
-
}
-
//---------------------------------------------------------------------------
Add some items to your list and test it. Clicking on the same column will switch between ascending and descending sort:
-
void __fastcall TForm1::FormCreate(TObject *Sender)
-
{
-
ColumnToSort=0;
-
//add some items to test ListView sorting...
-
TListItem *tli=ListView1->Items->Add();
-
tli->Caption="A item ";
-
tli->SubItems->Add("B subitem1");
-
tli->SubItems->Add("C subitem2");
-
-
tli=ListView1->Items->Add();
-
tli->Caption="B item ";
-
tli->SubItems->Add("E subitem1");
-
tli->SubItems->Add("A subitem2");
-
-
tli=ListView1->Items->Add();
-
tli->Caption="C item ";
-
tli->SubItems->Add("A subitem1");
-
tli->SubItems->Add("E subitem2");
-
-
tli=ListView1->Items->Add();
-
tli->Caption="D item ";
-
tli->SubItems->Add("C subitem1");
-
tli->SubItems->Add("D subitem2");
-
-
tli=ListView1->Items->Add();
-
tli->Caption="E item ";
-
tli->SubItems->Add("D subitem1");
-
tli->SubItems->Add("B subitem2");
-
}

related things:
Author: Raz | On February 17th, 2007 | C++ Builder, [ En ] | 3 Comments
Q: So what can I do next?
A: You can Buy me a Beer or Coffee. You can say Hi! or peek on the related stuff. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
Spammers: Beware of » my Dog! «





3 Responses to “Sorting a ListView”
May 10th, 2007 at 4:05 am
Great stuff, thanks! exactly the code snippit I was looking for
May 11th, 2007 at 9:55 pm
You're welcome, I'm glad to hear that this snippet helped you
November 17th, 2008 at 10:32 pm
oh my god!!! thxxx dude. im looking for this long time ago... its exactly what i need.
pd: in the code posted here the sorting its made descending/ascending, just changing:
#
if (ColTag==1)
#
Compare=CompareText(Str1,Str2);
#
else
#
Compare=CompareText(Str2,Str1);
for this:
#
if (ColTag==1)
#
Compare=CompareText(Str2,Str1);
#
else
#
Compare=CompareText(Str1,Str2);
the sorting change to ascending/descending, at least in my proyect.
greetings from venezuela, sorry about my english xD