Here's a small example. On the main form of a new Lazarus project drop five stringgrids. Double-click the form to generate an OnCreate handler and complete the code as follows:
unit mainMultipleGrids;
{$mode objfpc}{$H+}
interface
uses
Forms, Grids, sysutils;
type
{ TmultiGridForm }
TmultiGridForm = class(TForm)
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
StringGrid3: TStringGrid;
StringGrid4: TStringGrid;
StringGrid5: TStringGrid;
procedure FormCreate(Sender: TObject);
end;
var
multiGridForm: TmultiGridForm;
implementation
uses gridManager;
{$R *.lfm}
{ TmultiGridForm }
procedure TmultiGridForm.FormCreate(Sender: TObject);
var
i, c, r: integer;
grid: TStringGrid;
begin
Randomize;
for i:=0 to ControlCount-1 do
if (Controls[i] is TStringGrid) then
begin
grid:=TStringGrid(Controls[i]);
for r:=0 to grid.RowCount-1 do
for c:=0 to grid.ColCount-1 do
grid.Cells[c,r]:=IntToStr(Random(1000));
end;
manager.GridForm:=Self;
Caption:=Format('Cells sum = %d',[manager.ForEachCellDo]);
end;
end.
Then create a new unit (not a new form) named gridManager and complete its code as follows:unit gridManager;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Forms, Grids;
type
{ TGridManager }
TGridManager = class(TObject)
private
FGridForm: TForm;
public
function ForEachCellDo: integer;
property GridForm: TForm write FGridForm;
end;
var
manager: TGridManager = nil;
implementation
{ TGridManager }
function TGridManager.ForEachCellDo: integer;
var
i, r, c, n: integer;
grid: TStringGrid;
begin
Result:=0;
for i:=0 to FGridForm.ControlCount-1 do
if (FGridForm.Controls[i] is TStringGrid) then begin
grid:=TStringGrid(FGridForm.Controls[i]);
for r:=0 to grid.RowCount-1 do
for c:=0 to grid.ColCount-1 do
if TryStrToInt(Trim(grid.Cells[c,r]), n) then
Inc(Result, n);
end;
end;
initialization
if manager = nil then
manager:=TGridManager.Create;
finalization
FreeAndNil(manager);
end.
To print a grid, AFAIK you have to do the grunt work yourself:
- figure out printer page size
- calculate the maximum number of print lines possible for your font size per page
- format your grid data as line-based text
- send your data lines to the printer a page at a time
If you have image data as well, it is yet more tedious.