myCxGrid

//author: cxg
//操作cxgrid

unit myCxGrid;

interface

uses
  SysUtils, ComCtrls, Forms, Messages, Windows, ExtCtrls, StdCtrls
  , Graphics, Controls, Dialogs, Classes,
  cxCustomData, cxGraphics,
  cxFilter, cxData, cxDataStorage, cxDBData, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, cxGridExportLink
  , cxLookAndFeelPainters
  ;   

type
  TMyCxGrid = class(TObject)
    class procedure DrawIndicatorCell(
      Sender: TcxGridTableView; ACanvas: TcxCanvas;
      AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    class procedure DrawBackground(
      Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
      AViewInfo: TcxCustomGridCellViewInfo; var ADone: Boolean);
  end;

procedure ExpGridToXls(grid: TcxGrid);    //cxgrid操作
procedure CreateFooter(c: TcxGridDBTableView;
  const fieldList: string; typ: TcxSummaryKind);
procedure ShowLineNo(c: TcxGridDBTableView);

implementation

uses
  uLanguage
  ;

procedure ShowLineNo(c: TcxGridDBTableView);
begin
  c.OptionsView.Indicator := True;
  c.OptionsView.IndicatorWidth := 40;
  c.OnCustomDrawIndicatorCell := TMyCxGrid.DrawIndicatorCell;
  c.OnCustomDrawPartBackground := tmycxgrid.DrawBackground;
end;

procedure CreateFooter(c: TcxGridDBTableView;
  const fieldList: string; typ: TcxSummaryKind);
var
  i: Integer;
  f: TcxGridDBTableSummaryItem;
  l: TStringList;
begin
  l := TStringList.Create;
  l.DelimitedText := fieldList;
  l.Delimiter := ',';
  c.OptionsView.Footer := True;
  for i := 0 to c.ColumnCount - 1 do
  begin
    if l.IndexOf(c.Columns[i].DataBinding.FieldName) <> -1 then
    begin
      f := (c.DataController.Summary.FooterSummaryItems.Add) as TcxGridDBTableSummaryItem;
      f.FieldName := c.Columns[i].DataBinding.FieldName;
      f.Column := c.Columns[i];
      f.Kind := typ;
      case typ of
        skSum: f.Format := gethashstr('total');
        skCount: f.Format := gethashstr('count');
        skAverage: f.Format := gethashstr('average');
        skMin: f.Format := gethashstr('min');
        skMax: f.Format := gethashstr('max');
      end;
    end;
  end;
  l.Free;
end;

procedure ExpGridToXls(grid: TcxGrid);
var
  SaveDialog: TSaveDialog;
begin
  SaveDialog := TSaveDialog.Create(nil);
  with SaveDialog do
  begin
    Filter := 'excel|*.xls|html|*.html|xml|*.xml|text|.txt';
    if Execute then
    begin
      case FilterIndex of
        1: ExportGridToExcel(FileName, grid);
        2: ExportGridToHTML(FileName, grid);
        3: ExportGridToXML(FileName, grid);
        4: ExportGridToText(FileName, grid);
      end;
    end;
  end;
  SaveDialog.Free;
end;


{ TMyCxGrid }

class procedure TMyCxGrid.DrawBackground(
      Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
      AViewInfo: TcxCustomGridCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo is TcxGridGroupByBoxViewInfo then
  begin
    AViewInfo.Text:= GetHashStr('draggroup');
    ACanvas.FillRect(AViewInfo.Bounds);
  end; 
end;

class procedure TMyCxGrid.DrawIndicatorCell(Sender: TcxGridTableView;
  ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo;
  var ADone: Boolean);
var
  AIndicatorViewInfo: TcxGridIndicatorRowItemViewInfo;
  ATextRect: TRect;
  AFont: TFont;
  AFontTextColor, AColor: TColor;
begin
  AFont := ACanvas.Font;
  AColor := clBtnFace;
  AFontTextColor := clWindowText;
  if (AViewInfo is TcxGridIndicatorHeaderItemViewInfo) then
  begin
    ATextRect := AViewInfo.Bounds;
    InflateRect(ATextRect, -1, -1);

    Sender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.Bounds,
      ATextRect, [], cxBordersAll, cxbsNormal, taCenter, vaCenter,
      False, False, GetHashStr('lineno'), AFont, AFontTextColor, AColor);
    ADone := True;
  end;

  if not (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
    Exit;
  ATextRect := AViewInfo.ContentBounds;
  AIndicatorViewInfo := AViewInfo as TcxGridIndicatorRowItemViewInfo;
  InflateRect(ATextRect, -1, -1);

  if AIndicatorViewInfo.GridRecord.Selected then
    AFont.Style := ACanvas.Font.Style + [fsBold]
  else
    AFont.Style := ACanvas.Font.Style - [fsBold];

  Sender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.ContentBounds,
    ATextRect, [], [bBottom, bLeft, bRight], cxbsNormal, taCenter, vaCenter,
    False, False, IntToStr(AIndicatorViewInfo.GridRecord.Index + 1),
    AFont, AFontTextColor, AColor);
  ADone := True;
end;

end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值