1

我有在运行时为数字列创建摘要页脚的代码,但我无法显示组摘要结果。我查看了如何在运行时创建组摘要如何设置组摘要值以及如何在运行时创建摘要页脚?但我遇到了运行时错误:

带有消息“记录索引超出范围”的 EcxInvalidDataControllerOperation

当网格渲染时。

此代码接受任何 TcxGridDBTableView,因此很容易放入现有的 Delphi 表单中。

procedure SummaryGroup(ASummary: TcxDataSummary; AColumn: TcxGridDBColumn;
  AKind: TcxSummaryKind; AFormat: string);
var
  sumGroup: TcxDataSummaryGroup;
  link: TcxGridTableSummaryGroupItemLink; //TcxDataSummaryGroupItemLink;
  item: TcxGridDBTableSummaryItem;
begin
  AColumn.Summary.FooterKind := AKind;
  AColumn.Summary.FooterFormat := AFormat;
  sumGroup := ASummary.SummaryGroups.Add;
  link := sumGroup.Links.Add as TcxGridTableSummaryGroupItemLink;
  link.Column := AColumn;
  item := sumGroup.SummaryItems.Add as TcxGridDBTableSummaryItem;
  item.Column := AColumn;
  item.Kind := AKind;
  item.Position := spGroup;
  item.Format := AColumn.Summary.FooterFormat;
end;

procedure AutoAwesum(AView: TcxGridDBTableView);
var
  summary: TcxDataSummary;
  summing: Boolean;
  i: Integer;
  dc: TcxGridDBDataController;
  col: TcxGridDBColumn;

begin
  dc := AView.DataController;
  summing := False;
  summary := dc.Summary;
  summary.BeginUpdate;
  try
    summary.SummaryGroups.Clear;
    dc.BeginFullUpdate;
    try
      dc.GridView.ClearItems;
      dc.CreateAllItems;
      for i := 1 to AView.ColumnCount - 1 do
      begin
        col := AView.Columns[i];
        case col.DataBinding.Field.DataType of
          ftSmallint, ftInteger, ftWord, ftLargeint, ftAutoInc,
          ftLongWord, ftShortint:
            begin
              summing := true;
              SummaryGroup(summary, col, skSum, '#');
            end;
          ftFloat, ftBCD, ftFMTBcd, ftExtended, ftSingle:
            begin
              summing := true;
              SummaryGroup(summary, col, skSum, '#.##');
            end;
          ftCurrency:
            begin
              summing := true;
              SummaryGroup(summary, col, skSum, '$#.##');
            end;
        end;
      end;
      dc.DataModeController.GridMode := not summing;
      AView.OptionsView.Footer := summing;
      AView.OptionsView.GroupFooterMultiSummaries := summing;
      AView.OptionsView.GroupFooters := gfVisibleWhenExpanded;
    finally
      dc.EndFullUpdate;
    end;
  finally
    summary.EndUpdate;
  end;
end;

我错过了什么?谢谢。

4

1 回答 1

1

终于有机会回到这个话题了。正如预期的那样,更改很少而且很简单。下面的代码通常为网格中的每个数字列创建组摘要标题。我在您可能想要使用的代码中留下了一些注释掉的选项。

uses
  cxGridDBDataDefinitions;

procedure Summarize(ASummary: TcxDataSummary; AColumn: TcxGridDBColumn;
  AKind: TcxSummaryKind; AFormat: string);
var
  sumGroup: TcxDataSummaryGroup;
  link: TcxGridTableSummaryGroupItemLink;
  item: TcxGridDBTableSummaryItem;
begin
  AColumn.Summary.FooterKind := AKind;
  AColumn.Summary.FooterFormat := AFormat;
  AColumn.Summary.GroupKind := AKind;
  AColumn.Summary.GroupFormat := AFormat;
  AColumn.GroupIndex := -1;
  sumGroup := ASummary.SummaryGroups.Add;
  link := sumGroup.Links.Add as TcxGridTableSummaryGroupItemLink;
  link.Column :=  AColumn;
  item := sumGroup.SummaryItems.Add as TcxGridDBTableSummaryItem;
  item.Column := AColumn;
  item.Kind := skSum;
  item.Position := spGroup;
  item.Format := AColumn.Summary.FooterFormat;
end;

procedure AutoAwesum(AView: TcxGridDBTableView);
var
  summary: TcxDataSummary;
  summing: Boolean;
  i: Integer;
  dc: TcxGridDBDataController;
  col: TcxGridDBColumn;

begin
  dc := AView.DataController;
  summing := False;
  summary := dc.Summary;
  summary.BeginUpdate;
  try
    summary.SummaryGroups.Clear;
    dc.BeginFullUpdate;
    try
      dc.GridView.ClearItems;
      dc.CreateAllItems;
      for i := 1 to AView.ColumnCount - 1 do
      begin
        col := AView.Columns[i];
        case col.DataBinding.Field.DataType of
          ftSmallint, ftInteger, ftWord, ftLargeint, ftAutoInc,
          ftLongWord, ftShortint:
            begin
              summing := true;
              Summarize(summary, col, skSum, ',0');
            end;
          ftFloat, ftBCD, ftFMTBcd, ftExtended, ftSingle:
            begin
              summing := true;
              Summarize(summary, col, skSum, ',.00');
            end;
          ftCurrency:
            begin
              summing := true;
              Summarize(summary, col, skSum, '$,0.00');
            end;
        end;
      end;
//      dc.DataModeController.GridMode := not summing;
//      AView.OptionsView.Header := summing;
      AView.OptionsView.Footer := summing;
//      AView.OptionsView.GroupFooterMultiSummaries := summing;
//      AView.OptionsView.GroupFooters := gfVisibleWhenExpanded;
    finally
      dc.EndFullUpdate;
    end;
  finally
    summary.EndUpdate;
  end;
end;
于 2014-10-22T19:04:53.893 回答