Board index » delphi » DBGRID - Changing the color of every 2nd row

DBGRID - Changing the color of every 2nd row

Hi,

I would like to change the background color of every second
row in a DBGrid.  Do you know possibly how?

Cheers
Anton

 

Re:DBGRID - Changing the color of every 2nd row


This should do it, email me if you have trouble.  Make your OnDrawColumnCell
event look like the following:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
   with Sender as TDBGrid do begin
     Canvas.Brush.Color := clWhite;
     if (Column.Field.Index <> 0) and (Succ(Column.Field.Index) mod 2  = 0)
then begin
       Canvas.Brush.Color := clRed; {* the color we want every second row*}
     end;
     Canvas.FillRect(Rect);
     DBGrid1.Canvas.TextOut(Rect.Left, Rect.Top, Column.Field.Text);
   end;
end;

Quote
Anton wrote in message <380da343.16259...@news.is.co.za>...
>Hi,

>I would like to change the background color of every second
>row in a DBGrid.  Do you know possibly how?

>Cheers
>Anton

Re:DBGRID - Changing the color of every 2nd row


Re:DBGRID - Changing the color of every 2nd row


Actually it should look like this:

{* This says draw every second cell red, except when it is selected and has
focus, in which case it will be drawn in blue.  When a user enters data the
background is drawn white. *}
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
   with Sender as TDBGrid do begin
    if State = [gdSelected, gdFocused] then exit;
    Canvas.Brush.Color := clWhite;
    if (Column.Field.Index <> 0) and (Succ(Column.Field.Index) mod 2  = 0)
then begin
      Canvas.Brush.Color := clRed;
    end;
    Canvas.FillRect(Rect);
    DBGrid1.Canvas.TextOut(Rect.Left, Rect.Top, Column.Field.Text);
   end;
end;

Quote
Anton wrote in message <380da343.16259...@news.is.co.za>...
>Hi,

>I would like to change the background color of every second
>row in a DBGrid.  Do you know possibly how?

>Cheers
>Anton

Re:DBGRID - Changing the color of every 2nd row


I don't understand. Rows or columns ?
Quote
John Levander wrote in message <7ulh1r$64...@mailhost.prograph-inc.com>...
>Actually it should look like this:

>{* This says draw every second cell red, except when it is selected and has
>focus, in which case it will be drawn in blue.  When a user enters data the
>background is drawn white. *}
>procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
>  DataCol: Integer; Column: TColumn; State: TGridDrawState);
>begin
>   with Sender as TDBGrid do begin
>    if State = [gdSelected, gdFocused] then exit;
>    Canvas.Brush.Color := clWhite;
>    if (Column.Field.Index <> 0) and (Succ(Column.Field.Index) mod 2  = 0)
>then begin
>      Canvas.Brush.Color := clRed;
>    end;
>    Canvas.FillRect(Rect);
>    DBGrid1.Canvas.TextOut(Rect.Left, Rect.Top, Column.Field.Text);
>   end;
>end;

>Anton wrote in message <380da343.16259...@news.is.co.za>...
>>Hi,

>>I would like to change the background color of every second
>>row in a DBGrid.  Do you know possibly how?

>>Cheers
>>Anton

Other Threads