Example 38 : grouping and merged group headersThe advantage of cell merging is that it can also be used with grouping in order to create group header rows that look like real headers with text flowing over multiple columns. This header grouping is here achieved with a little extra code after the performing of the grouping itself. First a new column is inserted where the grouping expand / collaps nodes will be placed. Secondly, for every row where the grouping function added a node, the full row is merged with the MergeCells method.
procedure TForm1.GroupBtnClick(Sender: TObject); var i: Integer; begin AdvStringGrid1.InsertCols(0,1); // insert a new column to place the grouping nodes in AdvStringGrid1.ColWidths := 20; // make this a small column, just wide enough to fit the node graphic AdvStringGrid1.Group(1); // perform grouping based on column 1 for i := 1 to AdvStringGrid1.RowCount - 1 do begin if AdvStringGrid.IsNode(i) then AdvStringGrid1.MergeCells(1,i,AdvStringGrid1.ColCount,1); // Merge the full row end; end;
Before ungrouping the cell merging is removed again with the SplitCells method:
procedure TForm1.UnGroupBtnClick(Sender: TObject); var i: Integer; begin AdvStringGrid1.ExpandAll; for i := 1 to AdvStringGrid1.RowCount - 1 do begin if AdvStringGrid.IsNode(i) then AdvStringGrid1.SplitCells(1,i); // split cells again end; AdvStringGrid1.UnGroup; AdvStringGrid1.RemoveCols(0,1); // remove the place holder column for group nodes again end;
Delphi project & source files for downloading included in the main demos distribution for Delphi.