Search code examples
delphidelphi-2010tlistview

Delphi 2010: Group TListView items in vsReport ViewStyle


Firstly is this possible?

I have two issues - the fist is that I cannot get the groups to appear in the TListView when creating at run time. I'm using the following code:

lg := lvResults.Groups.Add;
lg.Header := 'New Starters';
lg.GroupID := 0;

The second is that even if I create groups at design time - I can see them in the form designer - they are absent in at run time - even before I refresh the data to add my own Items...

Additional: I have confirmed the answer below works on a virgin project. However it fails in the Project where I want to use it! I have replaced my TListView with a new one from the palette and no joy. The list view is on a tpagecontrol


Solution

  • The code below results in visible groups. Are you perhaps forgetting to set GroupView to True?

    procedure TMyForm.FormCreate(Sender: TObject);
    var
      Group: TListGroup;
      Item: TListItem;
    begin
      ListView1.ViewStyle := vsReport;
      ListView1.GroupView := True;
      ListView1.Columns.Add.Caption := 'My column';
      Group := ListView1.Groups.Add;
      Group.Header := 'My header';
      Item := ListView1.Items.Add;
      Item.GroupID := Group.GroupID;
      Item.Caption := 'My item';
    

    There is an code example in the Delphi documentation.