Board index » delphi » How to change color of a cell in dbgrid

How to change color of a cell in dbgrid

I would like to change the background color of a cell in a dbgrid
based on the value of another cell in the grid.  Something like this.
IF STATUS = OK THEN CHANGE ID BACKGROUND TO GREEN
ELSE CHANGE ID BACKGROUND TO RED
I would like to do this for all the rows of the grid.
Any suggestions?
Thanks,
Jim (jmille...@hotmail.com)
 

Re:How to change color of a cell in dbgrid


Quote
In article <3bfe6e5f.1756...@forums.borland.com>, Jim Miller wrote:
> I would like to change the background color of a cell in a dbgrid
> based on the value of another cell in the grid.  

Use an OnDrawColumnCell handler for the color change. Base your
descision on the datasets current record, do not try to figure out the
content of the other field from the grid. When OnDrawColumnCell is
called for a cell in a row the datasets current record will be for that
row.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Re:How to change color of a cell in dbgrid


On Fri, 23 Nov 2001 21:16:22 +0100, "Peter Below (TeamB)"

Quote
<100113.1...@compuXXserve.com> wrote:

Peter: Thanks for the info.  I have read the help for OnDrawColumnCell
and I still can't figure out how to change the color of the cell based
on the contents of the grid, or the dataset  contents.  Could you give
an example?
Thanks,
Jim
Quote
>In article <3bfe6e5f.1756...@forums.borland.com>, Jim Miller wrote:
>> I would like to change the background color of a cell in a dbgrid
>> based on the value of another cell in the grid.  

>Use an OnDrawColumnCell handler for the color change. Base your
>descision on the datasets current record, do not try to figure out the
>content of the other field from the grid. When OnDrawColumnCell is
>called for a cell in a row the datasets current record will be for that
>row.

>Peter Below (TeamB)  100113.1...@compuserve.com)
>No e-mail responses, please, unless explicitly requested!
>Note: I'm unable to visit the newsgroups every day at the moment,
>so be patient if you don't get a reply immediately.

Re:How to change color of a cell in dbgrid


Quote
In article <3bfef234.9836...@forums.borland.com>, Jim Miller wrote:
> Peter: Thanks for the info.  I have read the help for OnDrawColumnCell
> and I still can't figure out how to change the color of the cell based
> on the contents of the grid, or the dataset  contents.  Could you give
> an example?

Sure. Plop a TDatasource, TTable, TDBGrid on a form, wire together as
usual. Set the tables Databasename to DBDEMOS, the tablename to
animals.dbd. Set the tables Active property to true. Attach a handler to
the grids OnDrawColumnCell event:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);

Var
  grid: TDBGrid;

  function SizeAboveLimit: Boolean;
  begin
    Result := grid.DataSource.DataSet['size'] > 10;
  end;
begin
  grid := Sender as TDBGrid;
  { Don't mess with fixed or selected cells. }
  If ([gdFixed, gdSelected] * State) <> [] Then Exit;

  { If we are asked to draw a cell in the column for the Name field
    and the Size is > 10, color the cell yellow }
  If (CompareText( Column.FieldName, 'Name' ) = 0) and
     SizeAboveLimit
  Then Begin
    grid.Canvas.Brush.Color := clYellow;
    grid.DefaultDrawColumnCell( rect, datacol, column, state );
  end;
end;

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Re:How to change color of a cell in dbgrid


On Sat, 24 Nov 2001 01:08:14 GMT, jmille...@hotmail.com (Jim Miller)
wrote:

Quote
>Peter: Thanks for the info.  I have read the help for OnDrawColumnCell
>and I still can't figure out how to change the color of the cell based
>on the contents of the grid, or the dataset  contents.  Could you give
>an example?

The idea is that you prepare the Canvas, and then call
DefaultDrawColumnCell to actually draw the cell.

If SomeCondition then
  PrepareTheCanvas_1
else
  PrepareTheCanvas_2;

DefaultDrawColumnCell (Rect, DataCol, Column, State);

What you use as a condition, or how you prepare the canvas, is up to
you.

For instance, you may want to color the cell which surfaces a boolean
field, with a different color depending on the True/False value of the
data. You gain access to the underlying field data through
Column.Field. So:

Case Column.Field.AsBooolean of
  True:
    begin
      DbGrid1.Canvas.Brush.Color:= clYellow;
      DbGrid1.Canvas.Font.Color:= clMaroon;
    end;
  False:
    begin
      DbGrid1.Canvas.Brush.Color:= clBlue;
      DbGrid1.Canvas.Font.Color:= clWhite;
    end:
end; // case

DefaultDrawColumnCell (Rect, DataCol, Column, State);

or if you want to color the cell according to ranges for integer data:

TheIntegerValue:= Column.Field.AsInteger;

If TheIntegerValue < 100 then
  begin
    DbGrid1.Canvas.Brush.Color:= clYellow;
    DbGrid1.Canvas.Font.Color:= clMaroon;
  end
else if TheIntegerValue < 1000 then
  begin
    DbGrid1.Canvas.Brush.Color:= clBlue;
    DbGrid1.Canvas.Font.Color:= clWhite;
  end
else // TheIntegerValue >= 1000
  begin
    DbGrid1.Canvas.Brush.Color:= clGreen
    DbGrid1.Canvas.Font.Color:= clLime;
  end;

DefaultDrawColumnCell (Rect, DataCol, Column, State);

Of course, you can also handle all the other properties of the Canvas,
like the Font size, etc. You just prepare it at will, and then call
DefaultDrawColumnCell.

Manuel Algora
m...@encomix.es

Re:How to change color of a cell in dbgrid


On Sat, 24 Nov 2001 15:11:46 +0100, Manuel Algora <ceni...@wanadoo.es>
wrote:
Thanks to both of you for all the help.
Regards,
Jim
Quote
>On Sat, 24 Nov 2001 01:08:14 GMT, jmille...@hotmail.com (Jim Miller)
>wrote:

>>Peter: Thanks for the info.  I have read the help for OnDrawColumnCell
>>and I still can't figure out how to change the color of the cell based
>>on the contents of the grid, or the dataset  contents.  Could you give
>>an example?

>The idea is that you prepare the Canvas, and then call
>DefaultDrawColumnCell to actually draw the cell.

>If SomeCondition then
>  PrepareTheCanvas_1
>else
>  PrepareTheCanvas_2;

>DefaultDrawColumnCell (Rect, DataCol, Column, State);

>What you use as a condition, or how you prepare the canvas, is up to
>you.

>For instance, you may want to color the cell which surfaces a boolean
>field, with a different color depending on the True/False value of the
>data. You gain access to the underlying field data through
>Column.Field. So:

>Case Column.Field.AsBooolean of
>  True:
>    begin
>      DbGrid1.Canvas.Brush.Color:= clYellow;
>      DbGrid1.Canvas.Font.Color:= clMaroon;
>    end;
>  False:
>    begin
>      DbGrid1.Canvas.Brush.Color:= clBlue;
>      DbGrid1.Canvas.Font.Color:= clWhite;
>    end:
>end; // case

>DefaultDrawColumnCell (Rect, DataCol, Column, State);

>or if you want to color the cell according to ranges for integer data:

>TheIntegerValue:= Column.Field.AsInteger;

>If TheIntegerValue < 100 then
>  begin
>    DbGrid1.Canvas.Brush.Color:= clYellow;
>    DbGrid1.Canvas.Font.Color:= clMaroon;
>  end
>else if TheIntegerValue < 1000 then
>  begin
>    DbGrid1.Canvas.Brush.Color:= clBlue;
>    DbGrid1.Canvas.Font.Color:= clWhite;
>  end
>else // TheIntegerValue >= 1000
>  begin
>    DbGrid1.Canvas.Brush.Color:= clGreen
>    DbGrid1.Canvas.Font.Color:= clLime;
>  end;

>DefaultDrawColumnCell (Rect, DataCol, Column, State);

>Of course, you can also handle all the other properties of the Canvas,
>like the Font size, etc. You just prepare it at will, and then call
>DefaultDrawColumnCell.

>Manuel Algora
>m...@encomix.es

Other Threads