TUniDBGrid控件之Summary例子
(记录一下,方便以后备查)
在这个例子中,主要用到TUniDBGrid、TClientDataSet和TDataSource三个控件。
本文除去介绍使用TUniDBGrid控件之Summary外,TClientDataSet使用FieldDefs用于自定义的字段名表(即:不使用 data provider)参考:Delphi中ClientDataSet的用法小结。
例子的步骤如下:
- 新建一个UniGUI App,在MainForm上放入这三个控件:TUniDBGrid、TClientDataSet和TDataSource。
- 使用FieldDefs用于自定义的字段名表
修改第一个字段的属性
依次修改另外两个字段的属性
- 以FieldDefs中的字段名表为结构建立数据集
- 增加 Fields
建立后这模样:
- 设置显示属性
- 设置DataSource1的的dataset为ClientDataSet1。
- 设置UniDBGrid1的属性:DataSource为DataSource1;另外一个是:
- 增加列及修改列的属性
- 定义两个事件:
- Main.pas 的代码如下:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, uniGUITypes, uniGUIAbstractClasses,
uniGUIClasses, uniGUIRegClasses, uniGUIForm, Data.DB, Datasnap.DBClient,
uniGUIBaseClasses, uniBasicGrid, uniDBGrid;
type
TMainForm = class(TUniForm)
ClientDataSet1: TClientDataSet;
DataSource1: TDataSource;
ClientDataSet1Item: TStringField;
ClientDataSet1UnitPrice: TFloatField;
ClientDataSet1Quantity: TIntegerField;
UniDBGrid1: TUniDBGrid;
procedure UniFormCreate(Sender: TObject);
procedure UniDBGrid1ColumnSummary(Column: TUniDBGridColumn;
GroupFieldValue: Variant);
procedure UniDBGrid1ColumnSummaryResult(Column: TUniDBGridColumn;
GroupFieldValue: Variant; Attribs: TUniCellAttribs; var Result: string);
private
{ Private declarations }
public
{ Public declarations }
end;
function MainForm: TMainForm;
implementation
{$R *.dfm}
uses
uniGUIVars, MainModule, uniGUIApplication;
function MainForm: TMainForm;
begin
Result := TMainForm(UniMainModule.GetFormInstance(TMainForm));
end;
procedure TMainForm.UniDBGrid1ColumnSummary(Column: TUniDBGridColumn;
GroupFieldValue: Variant);
begin
if SameText(Column.FieldName, 'quantity') then
begin
if Column.AuxValue = NULL then Column.AuxValue:=0;
Column.AuxValue := Column.AuxValue + Column.Field.AsInteger;
end
else if SameText(Column.FieldName, 'unitprice') then
begin
if Column.AuxValue = NULL then Column.AuxValue:=0.0;
Column.AuxValue := Column.AuxValue + (Column.Field.AsFloat * ClientDataSet1Quantity.AsInteger);
end;
end;
procedure TMainForm.UniDBGrid1ColumnSummaryResult(Column: TUniDBGridColumn;
GroupFieldValue: Variant; Attribs: TUniCellAttribs; var Result: string);
var
I : Integer;
F : Real;
begin
if SameText(Column.FieldName, 'quantity') then
begin
I:=Column.AuxValue;
Result:=Format('Total Units: %d', [I]);
Attribs.Font.Style:=[fsBold];
Attribs.Font.Color:=clGreen;
end
else if SameText(Column.FieldName, 'unitprice') then
begin
F:=Column.AuxValue;
Result:='Total Cost: '+FmtSettings.CurrencyString + FormatCurr('0,0.00 ', F);
Attribs.Font.Style:=[fsBold];
Attribs.Font.Color:=clNavy;
end;
Column.AuxValue:=NULL;
end;
procedure TMainForm.UniFormCreate(Sender: TObject);
var
I : Integer;
begin
for I := 1 to 15 do
begin
with ClientDataSet1 do
begin
Append;
FieldByName('Item').AsString:='AG-'+Format('%0.5d', [I]);
FieldByName('UnitPrice').AsFloat:=Random(2000000)/100;
if I=10 then FieldByName('UnitPrice').AsFloat:=0/100;
FieldByName('Quantity').AsInteger:=Random(500);
end;
end;
end;
initialization
RegisterAppFormClass(TMainForm);
end.
标签:控件,begin,end,Column,TUniDBGrid,Summary,TMainForm,AuxValue
From: https://www.cnblogs.com/qiao-fu/p/17995454