DbGrid Zebrado

O exemplo abaixo mostra como deixar cada linha do componente DBGrid de uma cor diferente, dando assim um efeito zebrado. O controle é feito no evento OnDrawColumnCell.

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Db, DBTables, Grids, DBGrids, StdCtrls;

type
  TForm1 = class(TForm)
  Button1: TButton;
  DBGrid1: TDBGrid;
  Table1: TTable;
  DataSource1: TDataSource;
  procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
  private
  { Private declarations }
  public
  { Public declarations }
end;
var
Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  If odd(Table1.RecNo) then
  begin
    DBGrid1.Canvas.Font.Color:= clWhite;
    DBGrid1.Canvas.Brush.Color:= clGreen;
  end
  else
  begin
    DBGrid1.Canvas.Font.Color:= clBlack;
    DBGrid1.Canvas.Brush.Color:= clWhite;
  end;
  DBGrid1.Canvas.FillRect(Rect);
  DBGrid1.Canvas.TextOut(Rect.Left+2,Rect.Top,Column.Field.AsString);
end;
    

Autor : desconhecido

Voltar