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;