Cor de Coluna em StringGrid

Delphi

02/06/2003

Boa Tarde Pessoal

Estou precisando saber como eu coloco cor para cada coluna que desejar no Stringrid.

Grato Bruno Terossi


Bterossi

Bterossi

Curtidas 0

Respostas

Daniel Araújo

Daniel Araújo

02/06/2003

GOSTEI 0
Felipe Morais

Felipe Morais

02/06/2003

Bterossi,

Para alterar as cores do StringGrid é preciso codificar o evento OnDrawCell. O exemplo abaixo foi feito no Delphi 7 e funciona até na versão mais recente (Tokyo). Crie uma nova aplicação, insira um StringGrid e cole o código abaixo:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  // Se for a segunda coluna do Grid (a primeira coluna é 0);
  if (ACol = 1) then
  begin
    // Altera a cor do pincel para amarelo;
    TStringGrid(Sender).Canvas.Brush.Color := clYellow;
  end;

  // Se for a terceira coluna do Grid;
  if (ACol = 2) then
  begin
    // Altera a cor do pincel para amarelo;
    TStringGrid(Sender).Canvas.Brush.Color := clRed;
  end;

  // Preenche a célula do Grid com o pincel;
  TStringGrid(Sender).Canvas.FillRect(Rect);
end;

end.


Ainda do evento OnDrawCell você pode desenhar como o texto pode ser escrito, seus estilos e alinhamentos. Espero ter ajudado, abraço!
GOSTEI 0
POSTAR