设置dbgrideh的footer

{*******************************************************}
{                                                       }
{       设置footer                                      }
{                                                       }
{       版权所有 (C) 2007 咏南工作室(陈新光)          }
{                                                       }
{*******************************************************}

//==============================================================================
// 取得FOOTER的合计值pDbGrid.Columns[1].Footer.SumValue
// 比如要将金额转换成中文大写
//==============================================================================

unit uFooter;

interface

uses
  SysUtils,Graphics,Classes,DBGridEh;

//==============================================================================
// AFlag=1记录数
// AFlag=2合计
// AFlag=3两者都要
//==============================================================================

Procedure SetDbGridEhFoot(pDbGrid:TDBGridEh; pcFields: string;AFlag:Integer=3);

implementation

//------------------------------------------------------------------------------
// 功能:将指定分隔符分隔的字符串转换为字符串列表。
//             此函数在需要将
// 参数:
//             pcString : string; 字符串
//             pcChar   : string; 分隔符
//             pDesList :  TStringList   字符串列表
// 例如:
//  var tmpFldList : TStrings ;
//  begin
//      tmpFldList := TStringList.Create ;
//      StrToStringList(  Uppercase(pcFields), ',' , tmpFldList  );
//      ......
//      tmpFldList.Free ;
//  end;
//------------------------------------------------------------------------------

Procedure StrToStringList( pcString,pcChar:string;pDesList:TStringList );
var
  cAddStr,cSrcStr : string ;
  nPos : integer ;
begin
  pDesList.Clear ;

  cSrcStr := pcString ;
  while True do
  begin
     nPos := pos( pcChar, cSrcStr );
     if nPos = 0 then begin
        pDesList.Add( cSrcStr ) ;
        Exit ;
     end
     else begin
        cAddStr := copy( cSrcStr,1, nPos - 1 );
        pDesList.Add( cAddStr ) ;
        Delete( cSrcStr,1, nPos + length( pcChar )-1 );
     end;
  end;
end;

//--------------------------------------------------------------------------------
// 功能:设定 DbGridEh 合计行信息
// 参数: pDbGrid:TDBGridEh;
//        pcFields : string ; 字段列表,字段用逗号分隔
//        pvtType : TFooterValueType ;统计类型 TFooterValueType = (fvtNon, fvtSum, fvtAvg, fvtCount, fvtFieldValue, fvtStaticText);
// 引用:StrToStringList
// 例如:DbGridEhFoot( DbGridEh1, 'Number,Sum', fvtSum ); 设定数量和金额字段为合计统计
// AFlag=1记录数
// AFlag=2合计
// AFlag=3两者都要
//--------------------------------------------------------------------------------
Procedure SetDbGridEhFoot(pDbGrid:TDBGridEh; pcFields: string;AFlag:Integer=3);
var
  nFldLoop: integer;
  cFieldName: string;
  tmpFldList: TStringList;
begin
  if pDbGrid.Columns.Count=1 then exit;  //只有一列的不设置FOOTER
  pDbGrid.SumList.Active:=true;
  if AFlag=1 then                     //记录数
  begin
    pDbGrid.FooterRowCount:=1;
    pDbGrid.Columns[0].Footer.ValueType:=fvtStaticText;
    pDbGrid.Columns[0].Footer.Value:='记录数';
    pDbGrid.Columns[1].Footer.ValueType:=fvtCount;
  end;
  if AFlag=2 then                    //合计
  begin
    pDbGrid.FooterRowCount:=1;
    pDbGrid.Columns[0].Footer.ValueType:=fvtStaticText;
    pDbGrid.Columns[0].Footer.Value:='合计';
    tmpFldList:=TStringList.Create;
    try
      StrToStringList(Uppercase(pcFields),',',tmpFldList); 
      For nFldLoop:=0 to pDbGrid.Columns.Count-1 do
      begin
        cFieldName:=pDbGrid.Columns[nFldLoop].FieldName;
        if tmpFldList.IndexOf(uppercase(cFieldName))>=0 then
        begin
           pDbGrid.Columns[nFldLoop].Footer.ValueType:=fvtSum;
        end;
      end;
    finally
      FreeAndNil(tmpFldList);
    end;
  end;
  if AFlag=3 then                    //两者
  begin
    pDbGrid.FooterRowCount:=2;
    pDbGrid.Columns[0].Footers.Insert(0);
    pDbGrid.Columns[0].Footers.Insert(1);
    pDbGrid.Columns[0].Footers[0].ValueType:=fvtStaticText;
    pDbGrid.Columns[0].Footers[0].Value:='记录数';
    pDbGrid.Columns[0].Footers[1].ValueType:=fvtStaticText;
    pDbGrid.Columns[0].Footers[1].Value:='合计';
    pDbGrid.Columns[1].Footers.Insert(0);
    pDbGrid.Columns[1].Footers[0].ValueType:=fvtCount;
    tmpFldList:=TStringList.Create;
    try
      StrToStringList(Uppercase(pcFields),',',tmpFldList); 
      For nFldLoop:=0 to pDbGrid.Columns.Count-1 do
      begin
        cFieldName:=pDbGrid.Columns[nFldLoop].FieldName;
        if tmpFldList.IndexOf(uppercase(cFieldName))>=0 then
        begin
          pDbGrid.Columns[nFldLoop].Footers.Insert(0);
          pDbGrid.Columns[nFldLoop].Footers.Insert(1);
          pDbGrid.Columns[nFldLoop].Footers[1].ValueType:=fvtSum;
        end;
      end;
    finally
      FreeAndNil(tmpFldList);
    end;
  end;
end;

end.

posted on 2021-08-18 17:24  癫狂编程  阅读(181)  评论(0编辑  收藏  举报

导航

好的代码像粥一样,都是用时间熬出来的