Board index » delphi » D5 DBGrid row color

D5 DBGrid row color

It is possible to control colors on a row-basis with the standard DBGrid
that is delivered with D5?  I know it supports coloring by column, but I
want to alternate the colors on a row by row basis for readibility.
Is a 3rd party DBGrid my only option?
Thanks!
 

Re:D5 DBGrid row color


Quote
Bugs wrote:
> It is possible to control colors on a row-basis with the standard DBGrid
> that is delivered with D5?  I know it supports coloring by column, but I
> want to alternate the colors on a row by row basis for readibility.
> Is a 3rd party DBGrid my only option?

Here is a trick.

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
 if gdSelected in State then
   DBGrid1.Canvas.Brush.Color := clNavy {need that highlight for 'focus'}
 else if (Table1.RecNo mod 2) > 0 then
 begin
   DBGrid1.Canvas.Font.Color := clWhite;
   DBGrid1.Canvas.Brush.Color := clTeal;
 end;
 DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

Re:D5 DBGrid row color


"Mauro Patino" <M-Pat...@govst.edu> wrote in

Quote
> Here is a trick.

> procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
>   DataCol: Integer; Column: TColumn; State: TGridDrawState);
> begin
>  if gdSelected in State then
>    DBGrid1.Canvas.Brush.Color := clNavy {need that highlight for 'focus'}
>  else if (Table1.RecNo mod 2) > 0 then
>  begin
>    DBGrid1.Canvas.Font.Color := clWhite;
>    DBGrid1.Canvas.Brush.Color := clTeal;
>  end;
>  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
> end;

Thanks Mauro!  Worked perfect the first time!

Re:D5 DBGrid row color


"Mauro Patino" <M-Pat...@govst.edu> wrote

Quote
> Here is a trick.

> procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
>   DataCol: Integer; Column: TColumn; State: TGridDrawState);
> begin
>  if gdSelected in State then
>    DBGrid1.Canvas.Brush.Color := clNavy {need that highlight for 'focus'}
>  else if (Table1.RecNo mod 2) > 0 then
>  begin
>    DBGrid1.Canvas.Font.Color := clWhite;
>    DBGrid1.Canvas.Brush.Color := clTeal;
>  end;
>  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
> end;

Followup question for you Mauro.  This grid has it's dataset switched
occasionally, changing the data in the grid.  This screws up the alternating
coloring.  Any suggestions?
Thanks!

Re:D5 DBGrid row color


Quote
Bugs wrote:
> Followup question for you Mauro.  This grid has it's dataset switched
> occasionally, changing the data in the grid.  This screws up the alternating
> coloring.  Any suggestions?
> Thanks!

The row coloring is based on the Dataset

DataSource1.DataSet := Table1;

  if gdSelected in State then
    DBGrid1.Canvas.Brush.Color := clNavy {need that highlight for 'focus'}
  else if (Table1.RecNo mod 2) > 0 then  // <------------based on Table1
  begin
    DBGrid1.Canvas.Font.Color := clWhite;
    DBGrid1.Canvas.Brush.Color := clTeal;

if you change it

DataSource1.DataSet := Table2;

the row coloring is still based on Table1 and thus will not get the correct
effect.

Try using 'DataSource1.DataSet.RecNo' in the coloring event.  Now it will be
whatever dataset you use.

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
 if gdSelected in State then
   DBGrid1.Canvas.Brush.Color := clNavy {need that highlight for 'focus'}
 else if (DataSource1.DataSet.RecNo mod 2) > 0 then
 begin
   DBGrid1.Canvas.Font.Color := clWhite;
   DBGrid1.Canvas.Brush.Color := clTeal;
 end;
 DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

Hope it helps.

Mauro

Re:D5 DBGrid row color


"Mauro Patino" <M-Pat...@govst.edu> wrote in

Quote
> The row coloring is based on the Dataset

> DataSource1.DataSet := Table1;

>   if gdSelected in State then
>     DBGrid1.Canvas.Brush.Color := clNavy {need that highlight for 'focus'}
>   else if (Table1.RecNo mod 2) > 0 then  // <------------based on Table1
>   begin
>     DBGrid1.Canvas.Font.Color := clWhite;
>     DBGrid1.Canvas.Brush.Color := clTeal;

> if you change it

> DataSource1.DataSet := Table2;

> the row coloring is still based on Table1 and thus will not get the
correct
> effect.

> Try using 'DataSource1.DataSet.RecNo' in the coloring event.  Now it will
be
> whatever dataset you use.

> procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
>   DataCol: Integer; Column: TColumn; State: TGridDrawState);
> begin
>  if gdSelected in State then
>    DBGrid1.Canvas.Brush.Color := clNavy {need that highlight for 'focus'}
>  else if (DataSource1.DataSet.RecNo mod 2) > 0 then
>  begin
>    DBGrid1.Canvas.Font.Color := clWhite;
>    DBGrid1.Canvas.Brush.Color := clTeal;
>  end;
>  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
> end;

> Hope it helps.

> Mauro

Ah!  That makes sense and it worked perfectly!
Thanks again Mauro!

Other Threads