Example 66 : Using OnPrintNewPage to start a new page for new data in a column

vcl grid priting

Normally during the printing of a grid, it is the grid that decides when to start a new page, ie. when a row can no longer fit on the page being printed a new page is started. In some cases, it is desirable to customize this behaviour. In the sample below, it could be a feature request that one contains information from only one car brand. Therefore, it is required to force a new page during the printing of the grid. The OnPrintNewPage was specifically added for this purpose. This event is triggered before the printing of each row in the grid. It returns the actual row number that will be printed and has a boolean parameter NewPage through which a page break can be forced.

In this simple OnPrintNewPage event handler, the first normal column value of the row that is to be printed is compared with the previous value and when different, a new page is caused by setting the parameter NewPage = true:

procedure TForm1.AdvStringGrid1PrintNewPage(Sender: TObject; ARow: Integer;
var NewPage: Boolean);
  NewPage := false;
  if (ARow > 1) then
    NewPage := (AdvStringGrid1.Cells[0, ARow - 1] <> 
AdvStringGrid1.Cells[0, ARow]);

This shows how with a very little code, the page breaks during grid printing can be programmatically controlled.

Delphi project & source files for downloading included in the main demos distribution for Delphi.