DBGridEh用法总结三(PivotGrid的汉化)

数据库字段一般都是英文,不能直接展示给用户,而应该显示中文含义给用户。需要汉化两部分,一是设计用的字段汉化,二是表格区显示内容的汉化。


1、 字段汉化

设计工具(PivotGridToolBoxEh1)中显示字段中文含义,是通过数据集字段的DisplayLabel属性实现,只需要正确设置DisplayLabel即可。

一种方法运行期动态指定,是把字段名和中文含义保存在数据库中,需要的时候动态从数据库取。
另一种方法是设计期静态指定,是在设计期设置每个字段的中文含义。
我的程序中采用的是第一种方法。先创建数据表格(DBGridEh),并设置每一列的标题。在数据打开之后按表格的标题设置数据集字段的DisplayLable。
for I := 0 to DBGridEh1.FieldCount - 1 do
begin
  DBGridEh1.Columns[i].Field.DisplayLabel := DBGridEh1.Columns[i].Title.Caption;
end;
2、 表格汉化

显示统计结果的表格PivotGridEh中老是显示字段名,这样对普通用户使用很不方便。通过下面事件实现英文字段的汉化,显示中文含义,做如下修改即可实现。

procedure TCustomPivotGridEh.BuildGridArrayRowsMeasures;


for v := 0 to ActualValueFields.Count-1 do
    begin
      PivotCel := PivotGridArray[ip + ActualRowFlds.Count + v, ActualColFlds.Count + 1];
      PivotCel.CelType := sctValuesColCaptionEh;
      PivotCel.Value := //ActualValueFields[v].PivotFieldName;
      //下面这句话,可以实现表格中字段名的中文汉化
PivotDataSource.PivotFields.FindFieldByName(ActualValueFields[v].PivotFieldName).DisplayName;
    end;

备注:

这样修改后,用控件本身的打印功能,也可以正常显示汉化后的字段(按现在这样修修改之前也找到的别的方法,虽然可以解决表格上显示的问题,但是打印时还是英文,进而才找到现在的方案)。
打印代码如下:

procedure TQueryFrm.BtnPrintClick(Sender: TObject);
begin
  inherited;
  PivotGridEh1.PrintService.PageHeader.CenterText := self.Caption + '分组统计';
  PivotGridEh1.PrintService.Preview;
end;


3、导出Excel格式与显示不一致。修改下面过程

function ExportPivotGridEhToOleExcel(Grid:TCustomPivotGridEh;

 Options: TPivotGridExportAsOLEXLSOptionsEh

// ;const FileName: String = ''

  ):Variant;

//省略

  for i := 0 to Grid.FullColCount-1 do

  begin

    for j := 0 to Grid.FullRowCount-1 do

    begin

      PivotCel := Grid.VisPivotGridArray[i, j];

      if PivotCel.CelType in [sctAxisValueEh,sctValuesColCaptionEh] then

      begin

        Grid.FillAxisValueCellParams(DrPar, i,j, EmptyRect, [], PivotCel);

        FVarValues[j, i] := DrPar.DisplayValue;

      end else //确保导出内容与显示一模一样

        if PivotCel.CelType in [sctDataEh,sctHorzAggregateData, sctVertAggregateData] then

        begin

          Grid.FillDataCellParams(DrPar, i, j,EmptyRect, [], PivotCel);

          FVarValues[j, i] :=DrPar.DisplayValue;

        End  else

          FVarValues[j, i] := PivotCel.Value;//之前只有这一句

    end;

  end;

 

posted on 2021-06-22 19:33  癫狂编程  阅读(311)  评论(0编辑  收藏  举报

导航

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