Board index » delphi » StringGrids - Changing cell colors

StringGrids - Changing cell colors

Quote
>   r...@astro.ocis.temple.edu (Voyager) writes:

>  How can you change individual cell colors, without affecting other
>  cells?

You can do this from the OnDrawCell event for the grid. For example, the
sample piece of code below will color the background of the 3rd cell in
the 2nd row red. You can customize your grid to change font color to say
red for negative numbers, etc, etc, by checking the contents of Cell[Col,Row]
and setting canvas fonts, colors, etc from this event.

Hope this helps,
Greg
--------------------------------------------------

procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Longint;
  Rect: TRect; State: TGridDrawState);
begin
     if (Row = 1) and (Col = 2) then begin
        with StringGrid1 do begin
             Canvas.Font.Color := clBlack;
             Canvas.Brush.Color := clRed;
             Canvas.FillRect(Rect);
             Canvas.TextOut(Rect.Left,Rect.Top,Cells[Col,Row]);
             end;
        end;
end;

 

Re:StringGrids - Changing cell colors


In article <43821f$...@cronkite.ocis.temple.edu>, r...@astro.ocis.temple.edu (Voyager) says:

Quote

>How can you change individual cell colors, without affecting other
>cells?

Yesterday I bought Mastering Delphi by Cantu.
I think there are some clues on how
to do this on page 512. I have not mastered it
yet so I cannot give more detail.

regards Andrew Gunner, Melbourne

Other Threads