Search code examples
listviewdelphivcldelphi-10.1-berlin

How to ListView VCL collapse / expand by code?


I'm using Delphi 10.1 and VCL. How to collapse or expand all or specific Groups by code?


Solution

  • The expansion state is in the group's State property. For instance, to collapse the first group:

    ListView1.Groups[0].State := ListView1.Groups[0].State + [lgsCollapsed];
    

    There is no shortcut for processing all groups at once. You have to loop through them one at a time:

    for I := 0 to ListView1.Groups.Count-1 do
    begin
      ListView1.Groups[I].State := ListView1.Groups[I].State + [lgsCollapsed];
    end;