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;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章