DBGridEh实现类似于Excel的合计功能,请各位校验一下!

snownxtbaby 2015-02-10 09:27:04
Delphi7,这是我的项目里面的一个功能,客户要求可以像excel一样似的,选中多条数据,可以在下方的状态条显示其中某个字段的合计,下面这段代码实现了其要求,有如下设置:
1、dbgrideh的数据是动态生成的;
2、dbgrideh的options的dgediting=false,dgrowselect=true,dgmutiselect=true;
现在还有几小疑问:
当按键盘的Ctrel+A组合键选择全部数据后,不能实现全部数据的合计,只能用鼠标全部选择;

请各位来探讨一下~~~~~~~~~~~~~~~~~


procedure Tfm_ITime.DBTimeInfSelectionChanged(Sender: TObject);
var
sum,i:integer;
begin
self.GetTotal;
end;

procedure Tfm_ITime.GetTotal;
var
Total: double;
BM: TBookmarkStr;
tmpsum,i:integer;
begin
if not dm.ADO_TimeInf.Active then exit;
if dbtimeinf.DataSource.DataSet.RecordCount<1 then exit;
if dbtimeinf.Selection.SelectionType = gstNon then exit;
if dbtimeinf.Selection.SelectionType = gstRecordBookmarks then
if dbtimeinf.SelectedRows.Count=0 then exit;

tmpsum:=dm.GetDBField(dbtimeinf,'fmoney');
if tmpsum=-1 then exit;
{if tmpsum>=0 then
begin
dbtimeinf.Columns[tmpsum].Footer.Color:=clyellow;
dbtimeinf.Columns[tmpsum].Footer.ValueType:=fvtstatictext;
end;}

total:=0;

{if dbtimeinf.Selection.SelectionType = gstRecordBookmarks then
begin
for i:=0 to DBTimeInf.SelectedRows.Count-1 do
begin
dbtimeinf.DataSource.DataSet.GotoBookmark(pointer(DBTimeInf.SelectedRows.Items[i]));
total:=total+dbtimeinf.Fields[tmpsum].AsFloat;
end;
moneysum.Caption:='选中:'+inttostr(DBTimeInf.SelectedRows.Count)+',求和:'+floatToStr(Total);
end
else if DBTimeInf.Selection.SelectionType = gstAll then
begin
DBTimeInf.DataSource.DataSet.First;
while not DBTimeInf.DataSource.DataSet.Eof do
begin
total:=total+dbtimeinf.Fields[tmpsum].AsFloat;
DBTimeInf.DataSource.DataSet.Next;
end;
moneysum.Caption:='选中:'+inttostr(DBTimeInf.DataSource.DataSet.RecordCount)+',求和:'+floatToStr(Total);
end;}

with DBTimeInf do
begin
with Datasource.Dataset do
try
// BM := Bookmark;
SaveBookmark;
DisableControls;
try
case Selection.SelectionType of
gstRecordBookmarks:
begin
for I := 0 to Selection.Rows.Count-1 do
begin
Bookmark := Selection.Rows[I];
total:=total+dbtimeinf.Fields[tmpsum].AsFloat;
end;
moneysum.Caption:='选中:'+inttostr(DBTimeInf.SelectedRows.Count)+',求和:'+floatToStr(Total);
end;
gstAll:
begin
First;
while EOF = False do
begin
total:=total+dbtimeinf.Fields[tmpsum].AsFloat;
Next;
end;
moneysum.Caption:='选中:'+inttostr(DBTimeInf.SelectedRows.Count)+',求和:'+floatToStr(Total);
end;
end;
finally
//Bookmark := BM;
RestoreBookmark;
EnableControls;
end;
finally

end;
end;
end;
...全文
293 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
山东蓝鸟贵薪 2015-08-05
  • 打赏
  • 举报
回复
学习中

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧