//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.