(同一个世界,同一个梦想,交流学习C++Builder and Delphi XE10,传承c++builder and Delphi的魅力!欢迎各地朋友加入我的QQ群484979943,进群密码“BCB”,同时也请将该群号广为宣传,希望能够广集各方高手,共同进步。如需下载开发工具及源代码请加入我的QQ群。)
【阅读倡议】
1、有问题请留言;
2、没问题请点赞;
3、看连载请加群;
4、下源码请加群;
【开发工具】
1、C++Builder and Delphi 10.3.3
2、FMSoft_uniGUI_Complete_Professional_1.70.0.1531(正版)
本人主笔的国内第一本uniGUI教学案例代码已诞生,分为cbuilder和delphi两个版本,买代码送手册,需要的朋友可以加入我的QQ技术交流群484979943给我(群主)留言。资料简介:
https://www.meipian.cn/20b86ayo?share_from=others&user_id=64168117&uuid=a8a75af8c0cc31e6a21b8a79a2b07398&share_depth=1&first_share_uid=64168117&utm_medium=meipian_android&share_user_mpuuid=94b70e99e8b6986c71b270a9883befb2
在做报表时,有时候需要按照某个字段,将表格的所有记录分组,并且分别显示每组某个列的汇总值以及所有记录的汇总值。先将stu_score1和stu_score2两列的ShowSummary设置为True;UniDBGrid的Grouping用于记录分组,其下FieldName属性用于指定以哪一个字段做分组,Enabled属性用于控制是否分组;UniDBGrid的Summary用于汇总,其下GrandTotal用于设置是否显示所有记录的数据汇总行,GrandTotalAlign用于控制数据汇总行的显示位置,Enabled属性用于控制是否做汇总。为UniDBGrid1的OnColumnSummary事件、OnClumnSummaryResult、OnColumnSummaryTotal添加代码:
一、delphi源代码
- 列值累加
procedure TMainForm.UniDBGrid1ColumnSummary(Column: TUniDBGridColumn;
GroupFieldValue: Variant);
begin
if SameText(Column.FieldName, 'stu_score1') then
begin
if Column.AuxValue=NULL then
begin
Column.AuxValue:=0;
end;
Column.AuxValue:=Column.AuxValue + Column.Field.AsFloat;//小汇总累加
if Column.AuxValues[1]=NULL then
begin
Column.AuxValues[1]:=0;
end;
Column.AuxValues[1]:=Column.AuxValues[1] + Column.Field.AsFloat;//大汇总累加
end
else if SameText(Column.FieldName, 'stu_score2') then
begin
if Column.AuxValue=NULL then
begin
Column.AuxValue:=0.0;
end;
Column.AuxValue:=Column.AuxValue + Column.Field.AsFloat;
if Column.AuxValues[1]=NULL then
begin
Column.AuxValues[1]:=0.0;
end;
Column.AuxValues[1]:=Column.AuxValues[1] + Column.Field.AsFloat;
end;
end;
- 分组汇总
procedure TMainForm.UniDBGrid1ColumnSummaryResult(Column: TUniDBGridColumn;
GroupFieldValue: Variant; Attribs: TUniCellAttribs; var Result: string);
var
I : Real;
begin
if SameText(Column.FieldName, 'stu_score1') then
begin
I:=Column.AuxValue;
Result:=Format('语文总分 : %f', [I]);//显示小汇总
Attribs.Font.Style:=[fsBold];
Attribs.Font.Color:=clGreen;
end
else if SameText(Column.FieldName, 'stu_score2') then
begin
I:=Column.AuxValue;
Result:=Format('数学总分 : %f', [I]);
Attribs.Font.Style:=[fsBold];
Attribs.Font.Color:=clNavy;
end;
Column.AuxValue:=NULL;
end;
- 整体汇总
procedure TMainForm.UniDBGrid1ColumnSummaryTotal(Column: TUniDBGridColumn;
Attribs: TUniCellAttribs; var Result: string);
var
I : Real;
begin
if SameText(Column.FieldName, 'stu_score1') then
begin
I:=Column.AuxValues[1];
Result:=Format('语文总分 : %f', [I]);//显示大汇总
Attribs.Font.Style:=[fsBold];
Attribs.Font.Color:=clGreen;
end
else if SameText(Column.FieldName, 'stu_score2') then
begin
I:=Column.AuxValues[1];
Result:=Format('数学总分 : %f', [I]);
Attribs.Font.Style:=[fsBold];
Attribs.Font.Color:=clNavy;
end;
end;
二、cbuilder源代码
- 列值累加
void __fastcall TMainForm::UniDBGrid1ColumnSummary(TUniDBGridColumn *Column, Variant &GroupFieldValue)
{
if(Column->FieldName=="stu_score1")
{
if(Column->AuxValue.IsNull())
{
Column->AuxValue=0.0;
}
Column->AuxValue=Column->AuxValue+Column->Field->AsFloat;//分组小汇总
if(Column->AuxValues[1].IsNull())
{
Column->AuxValues[1]=0.0;
}
Column->AuxValues[1]=Column->AuxValues[1]+Column->Field->AsFloat;//整体大汇总
}
else if(Column->FieldName=="stu_score2")
{
if(Column->AuxValue.IsNull())
{
Column->AuxValue=0.0;
}
Column->AuxValue=Column->AuxValue+Column->Field->AsFloat;
if(Column->AuxValues[1].IsNull())
{
Column->AuxValues[1]=0.0;
}
Column->AuxValues[1]=Column->AuxValues[1]+Column->Field->AsFloat;
}
}
- 分组汇总
void __fastcall TMainForm::UniDBGrid1ColumnSummaryResult(TUniDBGridColumn *Column,
Variant &GroupFieldValue, TUniCellAttribs *Attribs, UnicodeString &Result)
{
String score1,score2;
TFontStyles fontStyle(1);//fsBold
if(Column->FieldName=="stu_score1")
{
score1=Column->AuxValue;
Result="语文总分:"+score1;
Attribs->Font->Style=fontStyle;
Attribs->Font->Color=clGreen;
}
else if(Column->FieldName=="stu_score2")
{
score2=Column->AuxValue;
Result="数学总分:"+score2;
Attribs->Font->Style=fontStyle;
Attribs->Font->Color=clGreen;
}
Column->AuxValue=NULL;
}
- 整体汇总
void __fastcall TMainForm::UniDBGrid1ColumnSummaryTotal(TUniDBGridColumn *Column,
TUniCellAttribs *Attribs, UnicodeString &Result)
{
String score1,score2;
TFontStyles fontStyle(1);//fsBold
if(Column->FieldName=="stu_score1")
{
score1=Column->AuxValues[1];
Result="语文总分:"+score1;
Attribs->Font->Style=fontStyle;
Attribs->Font->Color=clGreen;
}
else if(Column->FieldName=="stu_score2")
{
score2=Column->AuxValues[1];
Result="数学总分:"+score2;
Attribs->Font->Style=fontStyle;
Attribs->Font->Color=clGreen;
}
}