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

  • 相关阅读:
    “混乱有序”一首关于概率论的诗
    关于“混乱有序”理论的讨论 2020-09-20
    关于“混乱有序”理论的讨论 2020-08-30
    【Java】(有步骤!)模逆、模幂、十进制转十六进制、十六进制转十进制、xTime算法、LFSR画状态图、椭圆曲线加法、椭圆曲线乘法、获得椭圆曲线上的点
    算法导论第一课
    经典力学:第一课
    计算机科学及编程导论:第一课
    微积分重点:第十六至十八课
    微积分重点:第十四,十五课
    微积分重点:第十课至十三课
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2940634.html
Copyright © 2020-2023  润新知