Delphi 2010: Group TListView items in vsReport ViewStyle
Asked Answered
F

1

5

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

Footway answered 23/6, 2011 at 10:5 Comment(4)
Solution was that the Project had "Enable Runtime themes" disabled - without which Groups apparantly don't workFootway
that makes sense. It would have been daft to have backported them to Windows Classic.Pylon
@DanKelly What adverse affect does enabling "Enable Runtime themes" have, if any?Essex
In my case OwnerData was enabled: #10231679Roselinerosella
P
7

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.

Pylon answered 23/6, 2011 at 10:23 Comment(7)
Out of interest: did you try with vsReport as well? That's what is in the title of the question (would have been smarter to repeat it in the question as well)...Pact
@Marjan Didn't spot that bit thanks. Answer updated to cover that angle now.Pylon
Thanks, but doesn't work for me. I've removed all the columns, items, etc from the designed listview, replaced the code with the above, commenting all mine out, and all I get is "My Column" and "My Item"Footway
OK, just tested on a fresh project and it works... time to spot the difference... thanksFootway
GroupID is assigned automatically (starting at 0), so Group.GroupID := 0; is not required, and use Item.GroupID := Group.GroupID; insteadWoo
@Woo it's nice to be explicit and it gives the reader the cue for what is needed to introduce a second groupPylon
@DavidHeffernan GroupID will automatically be 1 for the second (and 2 for the third etc), manually assigning GroupID is possibly not a good idea, but I see your point. If we are going to be really pedantic s1 and s2 are never used. Also runtime themes need to be enabled for groups to be visible (which is what caught me up)Woo

© 2022 - 2024 — McMap. All rights reserved.