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:
parent
93ebddb4c5
commit
cac2143536
@ -15,7 +15,6 @@
|
|||||||
<XPManifest>
|
<XPManifest>
|
||||||
<DpiAware Value="True"/>
|
<DpiAware Value="True"/>
|
||||||
</XPManifest>
|
</XPManifest>
|
||||||
<Icon Value="0"/>
|
|
||||||
</General>
|
</General>
|
||||||
<BuildModes Count="1">
|
<BuildModes Count="1">
|
||||||
<Item1 Name="Default" Default="True"/>
|
<Item1 Name="Default" Default="True"/>
|
||||||
|
@ -6,8 +6,8 @@ object Form1: TForm1
|
|||||||
Caption = 'Form1'
|
Caption = 'Form1'
|
||||||
ClientHeight = 382
|
ClientHeight = 382
|
||||||
ClientWidth = 689
|
ClientWidth = 689
|
||||||
|
LCLVersion = '3.99.0.0'
|
||||||
OnCreate = FormCreate
|
OnCreate = FormCreate
|
||||||
LCLVersion = '2.3.0.0'
|
|
||||||
object DBGrid1: TDBGrid
|
object DBGrid1: TDBGrid
|
||||||
Left = 0
|
Left = 0
|
||||||
Height = 345
|
Height = 345
|
||||||
@ -42,8 +42,8 @@ object Form1: TForm1
|
|||||||
AutoSize = True
|
AutoSize = True
|
||||||
BorderSpacing.Around = 6
|
BorderSpacing.Around = 6
|
||||||
Caption = 'Print preview...'
|
Caption = 'Print preview...'
|
||||||
OnClick = Button1Click
|
|
||||||
TabOrder = 0
|
TabOrder = 0
|
||||||
|
OnClick = Button1Click
|
||||||
end
|
end
|
||||||
object cbShowTitles: TCheckBox
|
object cbShowTitles: TCheckBox
|
||||||
AnchorSideLeft.Control = Button1
|
AnchorSideLeft.Control = Button1
|
||||||
@ -56,8 +56,8 @@ object Form1: TForm1
|
|||||||
Width = 75
|
Width = 75
|
||||||
BorderSpacing.Around = 6
|
BorderSpacing.Around = 6
|
||||||
Caption = 'Show titles'
|
Caption = 'Show titles'
|
||||||
OnChange = cbShowTitlesChange
|
|
||||||
TabOrder = 1
|
TabOrder = 1
|
||||||
|
OnChange = cbShowTitlesChange
|
||||||
end
|
end
|
||||||
object cbShowIndicator: TCheckBox
|
object cbShowIndicator: TCheckBox
|
||||||
AnchorSideLeft.Control = cbShowTitles
|
AnchorSideLeft.Control = cbShowTitles
|
||||||
@ -71,8 +71,22 @@ object Form1: TForm1
|
|||||||
BorderSpacing.Left = 6
|
BorderSpacing.Left = 6
|
||||||
BorderSpacing.Around = 6
|
BorderSpacing.Around = 6
|
||||||
Caption = 'Show indicator column'
|
Caption = 'Show indicator column'
|
||||||
OnChange = cbShowIndicatorChange
|
|
||||||
TabOrder = 2
|
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
|
||||||
end
|
end
|
||||||
object DataSource1: TDataSource
|
object DataSource1: TDataSource
|
||||||
@ -99,6 +113,7 @@ object Form1: TForm1
|
|||||||
end
|
end
|
||||||
object BufDataset1: TBufDataset
|
object BufDataset1: TBufDataset
|
||||||
FieldDefs = <>
|
FieldDefs = <>
|
||||||
|
OnFilterRecord = BufDataset1FilterRecord
|
||||||
Left = 167
|
Left = 167
|
||||||
Top = 64
|
Top = 64
|
||||||
end
|
end
|
||||||
|
@ -17,14 +17,17 @@ type
|
|||||||
Button1: TButton;
|
Button1: TButton;
|
||||||
cbShowTitles: TCheckBox;
|
cbShowTitles: TCheckBox;
|
||||||
cbShowIndicator: TCheckBox;
|
cbShowIndicator: TCheckBox;
|
||||||
|
cbFiltered: TCheckBox;
|
||||||
DataSource1: TDataSource;
|
DataSource1: TDataSource;
|
||||||
DBGrid1: TDBGrid;
|
DBGrid1: TDBGrid;
|
||||||
GridPrinter1: TGridPrinter;
|
GridPrinter1: TGridPrinter;
|
||||||
GridPrintPreviewDialog1: TGridPrintPreviewDialog;
|
GridPrintPreviewDialog1: TGridPrintPreviewDialog;
|
||||||
Panel1: TPanel;
|
Panel1: TPanel;
|
||||||
|
procedure BufDataset1FilterRecord(DataSet: TDataSet; var Accept: Boolean);
|
||||||
procedure Button1Click(Sender: TObject);
|
procedure Button1Click(Sender: TObject);
|
||||||
procedure cbShowIndicatorChange(Sender: TObject);
|
procedure cbShowIndicatorChange(Sender: TObject);
|
||||||
procedure cbShowTitlesChange(Sender: TObject);
|
procedure cbShowTitlesChange(Sender: TObject);
|
||||||
|
procedure cbFilteredChange(Sender: TObject);
|
||||||
procedure FormCreate(Sender: TObject);
|
procedure FormCreate(Sender: TObject);
|
||||||
procedure GridPrinter1AfterPrint(Sender: TObject);
|
procedure GridPrinter1AfterPrint(Sender: TObject);
|
||||||
procedure GridPrinter1GetCellText(Sender: TObject; AGrid: TCustomGrid;
|
procedure GridPrinter1GetCellText(Sender: TObject; AGrid: TCustomGrid;
|
||||||
@ -132,6 +135,15 @@ begin
|
|||||||
end;
|
end;
|
||||||
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);
|
procedure TForm1.cbShowIndicatorChange(Sender: TObject);
|
||||||
begin
|
begin
|
||||||
if cbShowIndicator.Checked then
|
if cbShowIndicator.Checked then
|
||||||
@ -148,6 +160,19 @@ begin
|
|||||||
DBGrid1.Options := DBGrid1.Options - [dgTitles];
|
DBGrid1.Options := DBGrid1.Options - [dgTitles];
|
||||||
end;
|
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
|
{ The OnGetCellText fires whenever a cell is printed and the printer needs to
|
||||||
know the cell text. }
|
know the cell text. }
|
||||||
procedure TForm1.GridPrinter1GetCellText(Sender: TObject; AGrid: TCustomGrid;
|
procedure TForm1.GridPrinter1GetCellText(Sender: TObject; AGrid: TCustomGrid;
|
||||||
@ -181,13 +206,35 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
{ Since the DBGrid does not load all records, but we want to print all
|
{ 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;
|
procedure TForm1.GridPrinter1GetRowCount(Sender: TObject; AGrid: TCustomGrid;
|
||||||
var ARowCount: Integer);
|
var ARowCount: Integer);
|
||||||
|
var
|
||||||
|
accept: Boolean;
|
||||||
|
n: Integer;
|
||||||
begin
|
begin
|
||||||
BufDataset1.Last;
|
if BufDataset1.Filtered then
|
||||||
BufDataset1.First;
|
begin
|
||||||
ARowCount := BufDataset1.RecordCount;
|
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
|
if dgTitles in TDBGrid(AGrid).Options then
|
||||||
inc(ARowCount); // added 1 for the header row
|
inc(ARowCount); // added 1 for the header row
|
||||||
end;
|
end;
|
||||||
|
Loading…
Reference in New Issue
Block a user