Coloring the text or background of StringGrids

By setting the DefaultDrawing property to false and implementing the OnDrawCell event handler, you can add extensive customization to the StringGrid or DrawGrid control.  Here's an example of color the text of cell (2, 2) to red with a blue background.
 

KEYWORDS: FIllRect, Frame3D
 
 

//---------------------------------------------------------------------------
//in source...
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    StringGrid1->DefaultDrawing = false;
}

//OnDrawCell event handler
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, long Col, long Row,
        TRect &Rect, TGridDrawState State)
{
     //if it's a fixed row (headers)
     if (State.Contains(gdFixed))
     {
         StringGrid1->Canvas->Brush->Color = clBtnFace;
         StringGrid1->Canvas->Font->Color = clWindowText;
         StringGrid1->Canvas->FillRect(Rect);
         Frame3D(StringGrid1->Canvas, Rect, clBtnHighlight, clBtnShadow, 1);
     }

     //if the cell is selected
     else if (State.Contains(gdSelected))
     {
         StringGrid1->Canvas->Brush->Color = clHighlight;
         StringGrid1->Canvas->Font->Color = clHighlightText;
         StringGrid1->Canvas->FillRect(Rect);
     }

     //color cell (2, 2) only
     else if (Col == 2 && Row == 2)
     {
         StringGrid1->Canvas->Brush->Color = clBlue;
         StringGrid1->Canvas->Font->Color = clRed;
         StringGrid1->Canvas->FillRect(Rect);
     }

     //if normal
     else
     {
         StringGrid1->Canvas->Brush->Color = StringGrid1->Color;
         StringGrid1->Canvas->Font->Color = StringGrid1->Font->Color;
         StringGrid1->Canvas->FillRect(Rect);
     }

     AnsiString text = StringGrid1->Cells[Col][Row];
     StringGrid1->Canvas->TextRect(Rect, Rect.Left, Rect.Top, text);
}