Custom DBGrid cell formatting lazarus / 31-LCL ---------------------------------------------------------------------- tatamata · 2010-01-26 13:43 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: Code: [Select]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. ---------------------------------------------------------------------- theo · 2010-01-26 13:49 Without knowing exactly the method you use, it seems obvious: You change the canvas properties but never reset them. Imho you need an else case where you set font back to normal etc. ---------------------------------------------------------------------- tatamata · 2010-01-26 14:07 What exactly DefaultDrawColumnCell(Rect,DataCol,Column,State) do? ---------------------------------------------------------------------- theo · 2010-01-26 14:18 As I said, I don't know. But it seems obvious that it is not resetting the dbgrid canvas properties you have set before, so you have to do it yourself. ---------------------------------------------------------------------- JD · 2010-01-26 17:04 Try these tips. They are for Delphi but can be adapted to work in Lazarus http://delphi.about.com/od/usedbvcl/l/aa031699.htm ---------------------------------------------------------------------- paweld · 2010-01-26 17:09 use OnPrepareCanvas event: Code: [Select]procedure TForm1.DBGrid1PrepareCanvas(sender: TObject; DataCol: Integer;   Column: TColumn; AState: TGridDrawState); begin   if Column.FieldName='pole1' then   begin     if Column.Field.AsString='test' 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; end; this work. Best regards paweld ---------------------------------------------------------------------- tatamata · 2010-01-26 18:05 Thank you, Paweld! It works. What if I want to yellow brush for the whole row, instead of only the current cell? ---------------------------------------------------------------------- paweld · 2010-01-26 18:36 retrieves the value from ZQuery field: Code: [Select]procedure TForm1.DBGrid1PrepareCanvas(sender: TObject; DataCol: Integer;   Column: TColumn; AState: TGridDrawState); begin   {if Column.FieldName='pole1' then   begin     if Column.Field.AsString='test' then }     if ZQuery1.FieldByName('pole1').AsString='test' 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; end; sorry for my English Best regards paweld ---------------------------------------------------------------------- tatamata · 2010-01-26 19:20 Yeah, that's it. Thanks. The same is described in the article that JD linked... ---------------------------------------------------------------------- Fortuna1 · 2011-02-04 13:26 How   brush  if    ButtonStyle is                    cbsButtonColumn?? HOW change color  cbsButtonColumn in dbgrid   ?? Thanks ---------------------------------------------------------------------- darmawan · 2011-09-21 09:24 because if DefaultDrawing is set false, PrepareCanvas don't set canvas properties so you must declare it self. In the Grids unit u can see that.. Quoteprocedure TCustomGrid.PrepareCanvas(aCol, aRow: Integer; aState: TGridDrawState); var   AColor: TColor;   CurrentTextStyle: TTextStyle;   IsSelected: boolean; begin   if DefaultDrawing then begin     Canvas.Pen.Mode := pmCopy; . . .