Colorizing a TDBGrid

Abstract: Steve Koterski shows how to make your TDBGrid more colorful

Colorizing a TDBGrid

By Steve Koterski

The question often comes up about how to colorize a TDBGrid. Or, more specifically, how to conditionally color individual cells or rows in a TDBGrid. To do this, create a handler for the OnDrawDataCell event of the TDBGrid. In this event handler, check various factors and conditionally set the brush color for the grid's canvas to one color or another. Then, to draw the cell, call the TDBGrid component's DefaultDrawDataCell method.

The OnDrawDataCell event fires once for each cell drawn. To tell what field is currently being drawn, inspect the var parameter Field passed to the event handler. This is a TField type reference and its FieldName property reveals the drawn field's name.

Use this same Field parameter to determine the field's value when it is the field you wish to conditionally color. Use TField properties like AsString and AsInteger to get the field's value for use in comparisons in if..else constructs.

To determine whether the cell being drawn has focus or is just another displayed cell, check for the TGridDrawState constant gdFocused in the State parameter passed to the event handler. You might want to paint the focused cell and its font with a different color.

When calling the DefaultDrawDataCell method, use the var parameters passed to the OnDrawDataCell event handler as the arguments for DefaultDrawDataCell. The passed Rect parameter represents the rectangle in the TDBGrid component's canvas covered by the cell currently being drawn. The Field parameter is the field associated with the column, used by DefaultDrawDataCell to get the text for the cell. And the State parameter indicates whether the current cell is focused, fixed, selected, or simply a displayed cell. Set the value of the TDBGrid.Canvas.Brush.Color property to specify the background color for the cell and the TDBGrid.Canvas.Font.Color property to indicate the color of the text in the cell. When the DefaultDrawDataCell method is called, the cell is drawn using these color settings.

The example below uses the sample Paradox table Orders. Its Terms field is the one to be conditionally colored. This field contains two values: "FOB" (which will be colored the custom color clPaleGreen) and "Net 30" (colored the custom color clPaleRed). This event handler checks for the currently drawn cell having focus and, if it does, paints its area clBlack and its font clWhite. All cells that are not conditionally colored and do not have focus are painted with a background color of clWhite. All cells that do not have focus have a font color of clBlack.

{ Define custom colors }
  const
    clPaleGreen = TColor($CCFFCC);
    clPaleRed =   TColor($CCCCFF);
  begin
    { Set default font color }
    DBGrid1.Canvas.Font.Color := clBlack;
    { Is current cell the focused cell? }
    if (gdFocused in State) then begin
      DBGrid1.Canvas.Brush.Color := clBlack;
      DBGrid1.Canvas.Font.Color := clWhite;
    end
    { Not focused, is it the target field Terms? }
    else if (Field.FieldName = 'Terms') then
      { Check field content for one of two possible values }
      if (Field.AsString = 'FOB') then
        { If field contains "FOB" paint green }
        DBGrid1.Canvas.Brush.Color := clPaleGreen
      else
        { If field contains "Net 30" paint red }
        DBGrid1.Canvas.Brush.Color := clPaleRed
    else
      { Paint non-focused and non-target cells white }
      DBGrid1.Canvas.Brush.Color := clWhite;
    { Draw the cell! }
    DBGrid1.DefaultDrawDataCell(Rect, Field, State);
  end;

Server Response from: ETNASC01