Board index » delphi » DBGrid - OnDrawDataCell Problem

DBGrid - OnDrawDataCell Problem

Hello All,

I am trying to color differently the cells on my DBGrid depending on their
respective values.

I am using the code given at the bottom of this mail. This works fine but if
i am displaying just a few fields using the columns editor then the
colouring of the fields doesn't work. Any ideas?

Any help will be appreciated.

Thanks in advance.

Chaitanya

procedure TStockForm1.DBGrid1DrawDataCell(Sender: TObject;
  const Rect: TRect; Field: TField; State: TGridDrawState);
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 quantity? }
 else if (Field.FieldName = 'Quantity') then
  { Check field content for one of two possible values }
 if (Field.AsFloat <= 5) then
  { If field is less than 5 paint green }
   DBGrid1.Canvas.Brush.Color := clPaleGreen
  else
  { If field contains more than 5  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;

 

Re:DBGrid - OnDrawDataCell Problem


Hello Chaitanya,

OnDrawDataCell occurs for default, non-persistent columns only. Move your
code into OnDrawColumnCell.

--
Andrei Fomine.
Add full-blown clipboard and drag-and-drop capabilities to any control with
Transfer@once.
DbAltGrid allows multi-line layout and RTF text in a DBGrid descendant.
http://www.quasidata.com/

Quote
"Chaitanya" <sa...@vsnl.com> wrote in message news:3b8e82de_1@dnews...
> Hello All,

> I am trying to color differently the cells on my DBGrid depending on their
> respective values.

> I am using the code given at the bottom of this mail. This works fine but
if
> i am displaying just a few fields using the columns editor then the
> colouring of the fields doesn't work. Any ideas?

> Any help will be appreciated.

> Thanks in advance.

> Chaitanya

> procedure TStockForm1.DBGrid1DrawDataCell(Sender: TObject;
>   const Rect: TRect; Field: TField; State: TGridDrawState);
> 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 quantity? }
>  else if (Field.FieldName = 'Quantity') then
>   { Check field content for one of two possible values }
>  if (Field.AsFloat <= 5) then
>   { If field is less than 5 paint green }
>    DBGrid1.Canvas.Brush.Color := clPaleGreen
>   else
>   { If field contains more than 5  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;

Re:DBGrid - OnDrawDataCell Problem


Use the OnDrawColumnCell event. OnDrawDataCell exists only for backward
compatibility and should not be used.

--
Bill
(TeamB cannot answer questions received via email)

Other Threads