1.设置表头,是否允许多表头,设置是否只读。

  dbgrdh1.TitleFont.Color:=clBlue;
  dbgrdh1.Flat:=True;
  dbgrdh1.FixedColor:=clSkyBlue; //Flat为True背景渐变才会起作用
  dbgrdh1.TitleHeight:=30;
  dbgrdh1.TitleLines := 2;
  dbgrdh1.ColumnDefValues.Title.Alignment:=taCenter;
  dbgrdh1.UseMultiTitle := true;
  dbgrdh1.Columns[0].HideDuplicates := true;//是否隐藏重复
  dbgrdh1.RowSizingAllowed := True;
  DBgrdh1.DataSource := nil;

2.设置表格颜色

procedure Ttablefrm.dbgrdh1GetCellParams(Sender: TObject; Column: TColumnEh;
  AFont: TFont; var Background: TColor; State: TGridDrawState);
begin
  if dbgrdh1.DataSource <> nil then
  begin
    if dbgrdh1.SumList.RecNo mod 2 = 1 then
      Background := $00FFC4C4
    else
      Background := $00FFDDDD;
  end;
end;

3.添加合计。

  dbgrdh1.FooterRowCount := 1;
  dbgrdh1.SumList.Active := true;
  dbgrdh1.Columns[0].Footer.valuetype := fvtstatictext;//静态文本
  dbgrdh1.Columns[0].Footer.value := ‘合计:‘;

  for i := 0 to dbgrdh1.Columns.Count - 1 do
  begin
    if (i>4) and (i < 10) then
    begin
      dbgrdh1.Columns[i].Footer.ValueType := fvtSum;//合计sum
    end;
  end;

 

4.设置打印。

  PrintDBGridEh1.DBGridEh :=dbgrdh1;
  PrintDBGridEh1.SetSubstitutes([‘%[打印标题]‘,‘住院处结算单‘]);
  PrintDBGridEh1.Print;

5.设置导出(excel,html,txt)。

procedure OutToFile(IADO : TADOQuery; DgEh : TDBGridEh);

procedure Tfrm_bazl.OutToFile(IADO: TADOQuery; DgEh: TDBGridEh);
var
  ExpClass:TDBGridEhExportclass;
  Ext:String;
  FSaveDialog: TSaveDialog;
begin
  try
    if not IADO.IsEmpty then
    begin
      FSaveDialog := TSaveDialog.Create(Self);
      FSaveDialog.Filter:=‘Excel 文档 (*.xls)|*.XLS|Text files (*.txt)|*.TXT|Comma separated values (*.csv)|*.CSV|HTML file (*.htm)|*.HTM|Word 文档 (*.rtf)|*.RTF‘;
      if FSaveDialog.Execute and (trim(FSaveDialog.FileName)<>‘‘) then
      begin
        case FSaveDialog.FilterIndex of
            1: begin ExpClass := TDBGridEhExportAsXLS; Ext := ‘xls‘; end;
            2: begin ExpClass := TDBGridEhExportAsText; Ext := ‘txt‘; end;
            3: begin ExpClass := TDBGridEhExportAsCSV; Ext := ‘csv‘; end;
            4: begin ExpClass := TDBGridEhExportAsHTML; Ext := ‘htm‘; end;
            5: begin ExpClass := TDBGridEhExportAsRTF; Ext := ‘rtf‘; end;
        end;
        if ExpClass <> nil then
        begin
          if UpperCase(Copy(FSaveDialog.FileName,Length(FSaveDialog.FileName)-2,3)) <> UpperCase(Ext) then
            FSaveDialog.FileName := FSaveDialog.FileName + ‘.‘ + Ext;
            if FileExists(FSaveDialog.FileName) then
            begin
              if application.MessageBox(‘文件名已存在,是否覆盖   ‘, ‘提示‘, MB_ICONASTERISK or MB_OKCANCEL)<>idok then
                exit;
            end;
           Screen.Cursor := crHourGlass;
           SaveDBGridEhToExportFile(ExpClass,DgEh,FSaveDialog.FileName,true);
           Screen.Cursor := crDefault;
           MessageBox(Handle, ‘导出成功  ‘, ‘提示‘, MB_OK +
             MB_ICONINFORMATION);
          end;
      end;
      FSaveDialog.Destroy;
    end;
  except
    on e: exception do
    begin
      Application.MessageBox(PChar(e.message), ‘错误‘, MB_OK + MB_ICONSTOP);
    end;
  end;
end;