TStringGrid - color

Is there any way to set the color of an individual cell of a string grid in response to my codes action ie


if.....then cells[2,5]color := clGreen;
padyAsked:
Who is Participating?
 
ZifNabConnect With a Mentor Commented:
Hi Pady,

This is a very simplistic way and will get you further. (It colors the cell 'green' when it contains '4'.

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var s: string;
begin
  with (Sender as TStringGrid) do
   if Cells[ACol,ARow] = '4' then begin begin
    with Canvas do begin
      if not (gdSelected in state) then {this is not a selected cell}
       begin
        Pen.Style:=psClear;
        Brush.Style:=bsSolid;
        brush.Color := clGreen;
        {we draw a color rectangle in the cell}
        Rectangle(Rect.Left,Rect.Top,Rect.Right,Rect.Bottom);
        {next we need to put the text}
        s:=(Sender as TStringGrid).Cells[ACol,ARow];
        TextOut(Rect.Left+2,Rect.Top+2,s);
       end;
    end;
   end;
 end;
end;

Regards, Zif
0
All Courses

From novice to tech pro — start learning today.