You are here: Home » Builder » [ En ] » Sorting a ListView

Sorting a ListView

17 February |

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:

[cpp]

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);

}
//—————————————————————————
[/cpp]

Add some items to your list and test it. Clicking on the same column will switch between ascending and descending sort:
[cpp]
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″);
}
[/cpp]


You like this post? Be the first of my friends to give me a beer! click here. Thanks!
Filed under

Builder, [ En ]

| Tags:

5 Comments.

  1. Thank you, Raz! It’s great, and it’s helps me in my busyness.

  2. Hi dude,
    Your code snippet is perfect for my use and works very fine.
    Thanks very much

  3. 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

  4. You’re welcome, I’m glad to hear that this snippet helped you :)

  5. Great stuff, thanks! exactly the code snippit I was looking for :)