cxDBPivotGrid这么强大的控件,如果不能在运行期指定字段的统计类型,实在是太遗憾了(希望是我没有找到此功能),所以就动手添加。
1、 首先寻找切入口。一种方法是改控件本身,第二种方法是在控件外部实现对控件增加功能。在看源码寻找切入点的过程中,发现还是直接改控件本身较简单。
在单元中cxPivotGridAdvancedCustomization中有双击事件ListBoxDblClick(Sender:TObject),实现了行区和列区字段的排序功能,却没有修改数据区字段统计类型的功能。所以只需要修改此方法,增加对数据区字段的判断(红色字体为增加的代码),将实现的代码放在另外一个单元中,尽量减少对控件自身单元的修改(方便控件升级时该功能的迁移):
procedureTcxPivotGridAdvancedCustomizationForm.ListBoxDblClick(Sender: TObject);
var
AListBox: TcxFieldListListBox;
begin
if CanChangeFieldFilter then
begin
AListBox := TcxFieldListListBox(Sender);
UpdateHitTest;
if(AListBox.SelectedField <> nil) and (AListBox.SelectedField.Area =faData) then
EditPivotField(AListBox.SelectedField)
else
if (AListBox.SelectedField <> nil)and not PivotGrid.HitTest.HitAtFilter then
ChangeFieldSorting(AListBox.SelectedField);
end;
end;
2、 实现方法EditPivotField
functionEditPivotField(PivotField: TcxPivotGridField): Boolean;
var
f: TfEditPivotField;
begin
Result := False;
f := TfEditPivotField.Create(Application);
f.lFieldName.Caption := '统计字段: ' +PivotField.Caption;
f.lFieldName.Font.Style := f.Font.Style +[fsBold];
f.ListBox1.ItemIndex :=Ord(PivotField.SummaryType);
f.edtDisplay.Text :=PivotField.DisplayFormat;
if f.ShowModal = mrOk then
begin
PivotField.SummaryType :=TcxPivotGridSummaryType(f.ListBox1.ItemIndex);
PivotField.DisplayFormat :=f.edtDisplay.Text;
Result := True;
end;
f.Free;
end;
3、 效果图
运行期双击数据区的‘数量’或者‘金额实收’,即可弹出统计类型设置窗口。