Re: code for stringgrid alternating colours

Posted by Al Peterson on October 12, 2008

In Reply to code for stringgrid alternating colours posted by Wiona on October 10, 2008

: : : how can I give alternate background colors to the rows of a stringgrid?
: : ...
: : if (Row mod 0 = 0) and (Row 0) then
: : ...
: Thanks for your answer, but please can you give me some real source code example?

Sorry Wiona for my vague answer, didn't have the source code at hand, but hope now it's better.

In the Object Inspector, double click the OnDrawCell event of the stringgrid. Delphi creates the skeleton of a procedure, complete it like this:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (ACol <> 0) and (ARow <> 0) and (Arow mod 2 = 0) then begin
    StringGrid1.Canvas.Brush.Color := clYellow; // or other wanted color
    StringGrid1.Canvas.FillRect(Rect);
    StringGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2,
      StringGrid1.Cells[ACol, ARow]);
  end;
end;

Related articles