GridPrinter: Extend dbgrid2 demo for filtered dataset.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@9446 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz 2024-09-18 09:34:49 +00:00
parent 93ebddb4c5
commit cac2143536
3 changed files with 70 additions and 9 deletions

View File

@ -15,7 +15,6 @@
<XPManifest>
<DpiAware Value="True"/>
</XPManifest>
<Icon Value="0"/>
</General>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>

View File

@ -6,8 +6,8 @@ object Form1: TForm1
Caption = 'Form1'
ClientHeight = 382
ClientWidth = 689
LCLVersion = '3.99.0.0'
OnCreate = FormCreate
LCLVersion = '2.3.0.0'
object DBGrid1: TDBGrid
Left = 0
Height = 345
@ -42,8 +42,8 @@ object Form1: TForm1
AutoSize = True
BorderSpacing.Around = 6
Caption = 'Print preview...'
OnClick = Button1Click
TabOrder = 0
OnClick = Button1Click
end
object cbShowTitles: TCheckBox
AnchorSideLeft.Control = Button1
@ -56,8 +56,8 @@ object Form1: TForm1
Width = 75
BorderSpacing.Around = 6
Caption = 'Show titles'
OnChange = cbShowTitlesChange
TabOrder = 1
OnChange = cbShowTitlesChange
end
object cbShowIndicator: TCheckBox
AnchorSideLeft.Control = cbShowTitles
@ -71,8 +71,22 @@ object Form1: TForm1
BorderSpacing.Left = 6
BorderSpacing.Around = 6
Caption = 'Show indicator column'
OnChange = cbShowIndicatorChange
TabOrder = 2
OnChange = cbShowIndicatorChange
end
object cbFiltered: TCheckBox
AnchorSideLeft.Control = cbShowIndicator
AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = cbShowIndicator
AnchorSideTop.Side = asrCenter
Left = 350
Height = 19
Top = 9
Width = 296
BorderSpacing.Left = 6
Caption = 'Print only records with IntFields >=1000 and <= 2000'
TabOrder = 3
OnChange = cbFilteredChange
end
end
object DataSource1: TDataSource
@ -99,6 +113,7 @@ object Form1: TForm1
end
object BufDataset1: TBufDataset
FieldDefs = <>
OnFilterRecord = BufDataset1FilterRecord
Left = 167
Top = 64
end

View File

@ -17,14 +17,17 @@ type
Button1: TButton;
cbShowTitles: TCheckBox;
cbShowIndicator: TCheckBox;
cbFiltered: TCheckBox;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
GridPrinter1: TGridPrinter;
GridPrintPreviewDialog1: TGridPrintPreviewDialog;
Panel1: TPanel;
procedure BufDataset1FilterRecord(DataSet: TDataSet; var Accept: Boolean);
procedure Button1Click(Sender: TObject);
procedure cbShowIndicatorChange(Sender: TObject);
procedure cbShowTitlesChange(Sender: TObject);
procedure cbFilteredChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure GridPrinter1AfterPrint(Sender: TObject);
procedure GridPrinter1GetCellText(Sender: TObject; AGrid: TCustomGrid;
@ -132,6 +135,15 @@ begin
end;
end;
procedure TForm1.BufDataset1FilterRecord(DataSet: TDataSet; var Accept: Boolean
);
var
value: Integer;
begin
value := Dataset.FieldByName('IntField').AsInteger;
Accept := (value >= 1000) and (value <= 2000);
end;
procedure TForm1.cbShowIndicatorChange(Sender: TObject);
begin
if cbShowIndicator.Checked then
@ -148,6 +160,19 @@ begin
DBGrid1.Options := DBGrid1.Options - [dgTitles];
end;
procedure TForm1.cbFilteredChange(Sender: TObject);
begin
if cbFiltered.Checked then
begin
BufDataset1.OnFilterRecord := @BufDataset1FilterRecord;
BufDataset1.Filtered := true;
end else
begin
BufDataset1.Filtered := false;
BufDataset1.OnFilterRecord := nil;
end;
end;
{ The OnGetCellText fires whenever a cell is printed and the printer needs to
know the cell text. }
procedure TForm1.GridPrinter1GetCellText(Sender: TObject; AGrid: TCustomGrid;
@ -181,13 +206,35 @@ begin
end;
{ Since the DBGrid does not load all records, but we want to print all
of them, we must tell the printer the real number of rows to print. }
of them, we must tell the printer the real number of rows to print.
Note that depending on the type of dataset RecordCount may be wrong,
in particular when the dataset is filtered.}
procedure TForm1.GridPrinter1GetRowCount(Sender: TObject; AGrid: TCustomGrid;
var ARowCount: Integer);
var
accept: Boolean;
n: Integer;
begin
BufDataset1.Last;
BufDataset1.First;
ARowCount := BufDataset1.RecordCount;
if BufDataset1.Filtered then
begin
BufDataset1.DisableControls;
BufDataset1.First;
n := 0;
while not BufDataset1.EoF do
begin
BufDataset1FilterRecord(BufDataset1, accept);
if accept then inc(n);
BufDataset1.Next;
end;
BufDataset1.First;
BufDataset1.EnableControls;
end else
begin
BufDataset1.Last;
BufDataset1.First;
n := BufDataset1.RecordCount;
end;
ARowCount := n;
if dgTitles in TDBGrid(AGrid).Options then
inc(ARowCount); // added 1 for the header row
end;