I tried to program custom formatting for a cell in a DBGrid, so if the value is >0 that the font becomes bold and red and the background yellow. I disabled the DefaultDrawing property and wrote the following code:
procedure TFormMain.DBGridMultipleMRPComponentsDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Column.FieldName='ComponentShortageQuantity' then begin
if Column.Field.AsFloat>=0 then begin
with (Sender As TDBGrid) do begin
//Custom drawing
Canvas.Brush.Color:=clYellow;
Canvas.Font.Color:=clRed;
Canvas.Font.Style:=[fsBold];
end;
end;
end;
//Call default drawing
(Sender As TDBGrid).DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;Unfortunately, it doesn't work as I expected. All the rows and all the cells are now bolded, and font in the cell is red for all the rows.
What is wrong with my code?
Thanks.