### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [CLOSED] Merge cells (Stringgrid)  (Read 2131 times)

#### pcurtis

• Hero Member
• Posts: 841
##### [CLOSED] Merge cells (Stringgrid)
« on: December 04, 2021, 09:09:11 pm »
I found this code to merge cells. It half works, but the cells under the spanned cells are still selecable and the grid lines are visible.

How can I fix this?

Code: Pascal  [Select][+][-]
1. procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
2.   aRect: TRect; aState: TGridDrawState);
3. var
4.   i, x, y: Integer;
5. begin
6.   if gdFixed in aState then
7.     Exit;
8.   if not (ARow in [1, 3]) then
9.     Exit;
10.   {Draw row 1 with text from cell 1,1 spanning all cells in the row}
11.   with Sender as TStringGrid do
12.   begin
13.     {Extend rect to include grid line on right, if not last cell in row}
14.     if aCol < Pred(ColCount) then
15.       aRect.Right := aRect.Right + GridlineWidth;
16.     {Figure out where the text of the first cell would start relative to the
17.                         current cells rect}
18.     y := aRect.Top + 2;
19.     x := aRect.Left + 2;
20.     for i := 1 to aCol - 1 do
21.       x := x - ColWidths[i] - GridlineWidth;
22.     {Paint cell pale yellow}
23.     Canvas.Brush.Color := \$7FFFFF;
24.     Canvas.Brush.Style := bsSolid;
25.     Canvas.FillRect(aRect);
26.     {Paint text of cell 1,1 clipped to current cell}
27.     Canvas.TextRect(aRect, x, y, 'A rather long line which will span cells');
28.   end;
29. end;
30.
31.
« Last Edit: December 05, 2021, 09:40:29 am by pcurtis »
Windows 10
Laz 2.0.12
FPC 3.2.0

#### wp

• Hero Member
• Posts: 9157
##### Re: Merge cells (Stringgrid)
« Reply #1 on: December 04, 2021, 09:50:27 pm »
See the demo project in folder examples/gridexamples/merged_cells of your Lazarus installation.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### pcurtis

• Hero Member
• Posts: 841
##### Re: Merge cells (Stringgrid)
« Reply #2 on: December 04, 2021, 11:38:38 pm »
Thanks for the pointer.
Turned mcgrid into a component and it works great.
« Last Edit: December 05, 2021, 08:16:28 pm by pcurtis »
Windows 10
Laz 2.0.12
FPC 3.2.0