Source Code

Coloring Cells in a StringGrid

  • anonymous
  • Feb 22' 14
  • Delphi
  • 180
  • 199
  • 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);


//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;
 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 
      //Painting the Background

      //Painting the Text. Here you can improve the code with
      // using alignment and so on.
  • Tags : color, stringgrid, cell
comments powered by Disqus

Related Code

Export a MS Word Table to a TStringGrid

Color Picker