cxgrid实现分组统计和添加Footer

1.分组统计

var
  I:integer;
  lCol: TcxGridDBColumn;
  csglink : TcxDataSummaryGroupItemLink;
  csg : TcxDataSummaryGroup;
  csgItem : TcxDataSummaryItem;

----------------------------------------------------------

begin

  //给Grid添加一个新列 SECTION
   csg :=      cxgrdbtblvw_for_doctortv.DataController.Summary.SummaryGroups.Add;

//创建汇总项
 // csg.Links.Clear;
  lCol:= cxgrdbtblvw_for_doctortv.Columns[0];
  lcol.Caption := '';//此处用来设置分组的名称
//  lCol.Caption:= cxgrdbtblvw_for_doctortv.Columns[0].Caption;
  lCol.Visible := False; //设置不可见
  lCol.GroupIndex := 0; //设置按此列进行分组合计

  csglink := csg.Links.Add;
  csglink.ItemLink := cxgrdbtblvw_for_doctortv.Columns[0];

  cxgrdbtblvw_for_doctortv.DataController.Groups.FullExpand;
  cxgrdbtblvw_for_doctortv.OptionsView.Footer := true;
  cxgrdbtblvw_for_doctortv.OptionsView.GroupFooters:= gfVisibleWhenExpanded;

  for i := 0 to qry_for_tongji.FieldCount-1 do
  begin
    if i > 2 then
    begin
      cxgrdbtblvw_for_doctortv.Columns[i].Summary.groupkind := skSum;
      cxgrdbtblvw_for_doctortv.Columns[i].Summary.groupFormat := ':0';
      csgitem := csg.SummaryItems.Add;
      csgitem.ItemLink := cxgrdbtblvw_for_doctortv.Columns[i];   //汇总字段1
      csgitem.Kind := skSum;
      csgItem.Format := '#,0.00;-#,0.00; #';
      csgItem.Position := spFooter;
    end;
    if i = 1 then
    begin
      cxgrdbtblvw_for_doctortv.Columns[i].Summary.groupkind := skSum;
      cxgrdbtblvw_for_doctortv.Columns[i].Summary.groupFormat := ':0';
      csgitem := csg.SummaryItems.Add;
      csgitem.ItemLink := cxgrdbtblvw_for_doctortv.Columns[i];   //汇总字段1
      csgitem.Kind := skMin;
      csgItem.Format := '合计:';
      csgItem.Position := spFooter;
    end;
  end;

  cxgrdbtblvw_for_doctortv.OptionsView.GroupSummaryLayout := gslAlignWithColumnsAndDistribute;
  dxbrbtn4.Enabled := True;

end;

2.添加footer的函数

procedure Tfrmtongji.CreateSumFooter(cxTableView: TcxGridDBTableView;
  const fieldList: string);
var
  i: Integer;
  f: TcxGridDBTableSummaryItem;
  l: TStringList;
begin
  l := TStringList.Create;
  l.DelimitedText := fieldList;
  l.Delimiter := ',';
  cxTableView.OptionsView.Footer := True;
  for i := 0 to cxTableView.ColumnCount - 1 do
  begin
    if l.IndexOf(cxTableView.Columns[i].DataBinding.FieldName) <> -1 then
    begin
      f := (cxTableView.DataController.Summary.FooterSummaryItems.Add) as TcxGridDBTableSummaryItem;
      f.FieldName := cxTableView.Columns[i].DataBinding.FieldName;
      f.Column := cxTableView.Columns[i];
      if (f.FieldName <> '医师编码') and (f.FieldName <> '医师名称') and (f.fieldName <> '科室名称') then
      begin
          f.Kind := skSum;
          f.Format := '#,0.00;-#,0.00; #';
      end
      else if (f.FieldName = '医师编码')  then
      begin
        f.Kind := skcount;
        f.Format := '合计:';
      end;
    end;
  end;
  l.Free;
end;

3.手工添加footer

  A:先手动添加一个column,然后设置所添加footer的column,然后就可以在ongettext()中添加 AText := '合计';

  
  在Footer的第一列显示[合计:]  
  加一个Summary项,Column设为Grid的第一列,Kind设为skNone  
  在该Summary项的OnGetText事件中,输入:  
  procedure   TFormExpense.tvExpenseTcxGridDBDataControllerTcxDataSummaryFooterSummaryItems2GetText(  
      Sender:   TcxDataSummaryItem;   const   AValue:   Variant;   AIsFooter:   Boolean;  
      var   AText:   String);  
  begin  
      AText   :=   '合计:';  
  end;  
   
  按条件汇总:  
  在TableView的DataController->Summary->FooterSummary->OnSummary事件中,输入:  
  procedure   TFormExpense.tvExpenseDataControllerSummaryFooterSummaryItemsSummary(  
      ASender:   TcxDataSummaryItems;   Arguments:   TcxSummaryEventArguments;  
      var   OutArguments:   TcxSummaryEventOutArguments);  
  begin  
      //得到字段名   TcxDBDataSummaryItem(Arguments.SummaryItem).FieldName;  
      if   (ASender.DataController.Values[Arguments.RecordIndex,   tvExpenseLevel.Index]   >   1)       //只统计Level列=1的值  
          and   (TcxDBDataSummaryItem(Arguments.SummaryItem).Kind   =   skSum)   then  
          OutArguments.Value   :=   0; //Level   >   1的统计值设为0 

 

你可能感兴趣的:(统计,footer,合计,cxgrid)