Source Code

Coloring Cells in a DBGrid

  • anonymous
  • Feb 22' 14
  • Delphi
  • 182
  • 197
  • United States

It's much easier to give color to DBGrids. Here you have to use the "OnDrawColumnCell"-Event. The following example is coloring the Cells of Column "Status" when the value is not "a".

If you want to color the whole line you only have to delete the "If..." statement (see below)

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
                                       DataCol: Integer; Column: TColumn; 
                                       State: TGridDrawState);
  clPaleGreen = TColor($CCFFCC);
  clPaleRed =   TColor($CCCCFF);

If Column.FieldName = 'Status' then  //Remove this line, if you want
                                     //to highlight the whole line

If Column.Field.Dataset.FieldbyName('Status').AsString  'a'
   If (gdFocused in State) //does the cell have the focus?
    then dbgrid1.canvas.brush.color := clBlack          //focused
    else dbgrid1.canvas.brush.color := clPaleGreen;  //not focused

//Now let's paint the cell using a Default-Method:
  • Tags : color, stringgrid, cell
comments powered by Disqus

Related Code

Coloring Cells in a StringGrid

Export a MS Word Table to a TStringGrid

Color Picker