Source Code



Coloring Cells in a StringGrid

  • anonymous
  • Feb 22' 14
  • Delphi
  • 180
  • 196
  • United States

The Source Code


procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);

Const   //define your color here. Of course you 
        //can use default colors too.
  clPaleGreen = TColor($CCFFCC);
  clPaleRed =   TColor($CCCCFF);

begin

//Does the cell have the focus you have to paint it with other colors
 if (gdFocused in State) then begin     
   StringGrid1.Canvas.Brush.Color := clBlack;
   StringGrid1.Canvas.Font.Color := clWhite;
 end
 else  //Does the cell have NOT the focus you can use 
       //your personal colors here

   if ACol = 2   //the second Column should be 
                 //green, the other cells red
    then StringGrid1.Canvas.Brush.color := clPaleGreen
    else StringGrid1.canvas.brush.Color := clPaleRed;

//Now Paint the cells, but only, if the cell isn't the Title- Row/Column
//This of course depends whether you have title-Row/Columns or not.

If (ACol  0) and (ARow0) then 
  begin 
      //Painting the Background
    StringGrid1.canvas.fillRect(Rect);

      //Painting the Text. Here you can improve the code with
      // using alignment and so on.
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;
  • Tags : color, stringgrid, cell
comments powered by Disqus

Related Code

Export a MS Word Table to a TStringGrid

Color Picker



Category