Como mudar a cor da célula ou da fonte de um DBGrid?
Há casos em que gostaríamos de personalizar as cores de um DBGrid,
seja a segue uma serie de possibilidades:
Exemplo de como trocar a cor da fonte.
1
2
3
4
5
6
7
8
9
10
11
| procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const [Ref] Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if ( True ) then // <- Troque pelo seu teste
begin
(Sender as TDBGrid).Canvas.Font.Color:= clred;
(Sender as TDBGrid).Canvas.Font.Style := [fsBold];
(Sender as TDBGrid).Canvas.FillRect(Rect);
(Sender as TDBGrid).DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
end; |
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const [Ref] Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if ( True ) then // <- Troque pelo seu teste
begin
(Sender as TDBGrid).Canvas.Font.Color:= clred;
(Sender as TDBGrid).Canvas.Font.Style := [fsBold];
(Sender as TDBGrid).Canvas.FillRect(Rect);
(Sender as TDBGrid).DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
end;
Outros casos, queremos trocar a cor do fundo:
Exemplo de como trocar a cor de fundo da célula
1
2
3
4
5
6
7
8
9
10
11
| procedure TForm1.DBGrid2DrawColumnCell(Sender: TObject; const [Ref] Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if ( True ) then // <- Troque pelo seu teste
begin
(Sender as TDBGrid).Canvas.Brush.Color:= clred;
(Sender as TDBGrid).Canvas.Font.Style := [fsBold];
(Sender as TDBGrid).Canvas.FillRect(Rect);
(Sender as TDBGrid).DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
end; |
procedure TForm1.DBGrid2DrawColumnCell(Sender: TObject; const [Ref] Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if ( True ) then // <- Troque pelo seu teste
begin
(Sender as TDBGrid).Canvas.Brush.Color:= clred;
(Sender as TDBGrid).Canvas.Font.Style := [fsBold];
(Sender as TDBGrid).Canvas.FillRect(Rect);
(Sender as TDBGrid).DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
end;
Ainda é possível especificar as cores por linhas e colunas:
Exemplo de teste com colunas e(ou) células
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| procedure TForm1.DBGrid3DrawColumnCell(Sender: TObject; const [Ref] Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
{ Teste da coluna } // pode ser testado o titulo da colula -> Column.Title
if (Column.Field = 'SALDO') then
begin
{ Seu código }
end;
{ Teste por linha }
if (ClientDataSet1.FieldByName('SALDO').Value > 0) then
begin
{ Seu código }
end;
end; |
procedure TForm1.DBGrid3DrawColumnCell(Sender: TObject; const [Ref] Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
{ Teste da coluna } // pode ser testado o titulo da colula -> Column.Title
if (Column.Field = 'SALDO') then
begin
{ Seu código }
end;
{ Teste por linha }
if (ClientDataSet1.FieldByName('SALDO').Value > 0) then
begin
{ Seu código }
end;
end;
Também há como deixar o DBGrid zebrado e este exemplo está na dica abaixo:
http://showdelphi.com.br/dica-como-deixar-um-dbgrid-zebrado-ou-colorido-delphi
Lembrando que isso não se aplica caso você utilizar algum style diferente do “Windows”,
pois as cores, serão as cores do style.
Espero ter ajudado a todos!
-
Giovani Da Cruz
-
9.848 views
- 0 comentários
- 2 de fevereiro de 2016
Está gostando do conteúdo? Considere pagar um cafezinho para nossa equipe!
Posts Relacionados - Continue Aprendendo