Delphi – TListView column sort (sorted by the first two columns)

I use Delphi 2010 and TListView to list names and other data. The first two columns are Last Name&Name

Caption = Last Name
SubItems[0] = First Name

How to sort ListView by these two columns? These are just the columns that Listview will sort by column, I want to always keep the sorting (when adding, editing, deleting items)

How can I do this?

Set SortType to’stBoth’, and implement the OnCompare event handler. Example:

procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
var
S1, S2: string;
begin
S1 := Item1.Caption;
if Item1.SubItems.Count> 0 then
S1 := S1 + Item1.SubItems[ 0];
S2 := Item2.Caption;
if Item2.SubItems.Count> 0 then
S2 := S2 + Item2.SubItems[0];

Compare := CompareText(S1, S2);
end;

I use Delphi 2010 and TListView to list names and other data. The first two columns are Last Name&Name

Caption = Last Name
SubItems[0] = First Name

How to pass this To sort ListView by two columns? These are just the columns that Listview will sort by column, I want to always keep the sorting (when adding, editing, deleting items)

How can I do this?

Set SortType to’stBoth’ and implement the OnCompare event handler. Example:

< pre>procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
var
S1, S2: string;
begin
S1 := Item1.Caption;
if Item1.SubItems.Count> 0 then
S1 := S1 + Item1.SubItems[0];
S2 := Item2. Caption;
if Item2.SubItems.Count> 0 then
S2 := S2 + Item2.SubItems[0];

Compare := CompareText(S1, S2);< br />end;

Leave a Comment

Your email address will not be published.