Source Code



Coloring Cells in a DBGrid

  • anonymous
  • Feb 22' 14
  • Delphi
  • 182
  • 194
  • 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);
const
  clPaleGreen = TColor($CCFFCC);
  clPaleRed =   TColor($CCCCFF);
begin

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

If Column.Field.Dataset.FieldbyName('Status').AsString  'a'
  then
   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:
 dbgrid1.DefaultDrawColumnCell(rect,DataCol,Column,State)
end;
  • 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



Category