Recent

Author Topic: Custom DBGrid cell formatting  (Read 30259 times)

tatamata

  • Hero Member
  • *****
  • Posts: 804
    • ZMSQL - SQL enhanced in-memory database
Custom DBGrid cell formatting
« on: January 26, 2010, 02:43:31 pm »
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.
« Last Edit: January 26, 2010, 02:46:40 pm by tatamata »

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: Custom DBGrid cell formatting
« Reply #1 on: January 26, 2010, 02:49:33 pm »
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

  • Hero Member
  • *****
  • Posts: 804
    • ZMSQL - SQL enhanced in-memory database
Re: Custom DBGrid cell formatting
« Reply #2 on: January 26, 2010, 03:07:44 pm »
What exactly DefaultDrawColumnCell(Rect,DataCol,Column,State) do?

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: Custom DBGrid cell formatting
« Reply #3 on: January 26, 2010, 03:18:01 pm »
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

  • Hero Member
  • *****
  • Posts: 1908
Re: Custom DBGrid cell formatting
« Reply #4 on: January 26, 2010, 06:04:00 pm »
Try these tips. They are for Delphi but can be adapted to work in Lazarus

http://delphi.about.com/od/usedbvcl/l/aa031699.htm
Linux Mint - Lazarus 4.0/FPC 3.2.2,
Windows - Lazarus 4.0/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

paweld

  • Hero Member
  • *****
  • Posts: 1516
Re: Custom DBGrid cell formatting
« Reply #5 on: January 26, 2010, 06:09:50 pm »
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
Best regards / Pozdrawiam
paweld

tatamata

  • Hero Member
  • *****
  • Posts: 804
    • ZMSQL - SQL enhanced in-memory database
Re: Custom DBGrid cell formatting
« Reply #6 on: January 26, 2010, 07:05:20 pm »
Thank you, Paweld! It works.

What if I want to yellow brush for the whole row, instead of only the current cell?

paweld

  • Hero Member
  • *****
  • Posts: 1516
Re: Custom DBGrid cell formatting
« Reply #7 on: January 26, 2010, 07:36:00 pm »
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
Best regards / Pozdrawiam
paweld

tatamata

  • Hero Member
  • *****
  • Posts: 804
    • ZMSQL - SQL enhanced in-memory database
Re: Custom DBGrid cell formatting
« Reply #8 on: January 26, 2010, 08:20:49 pm »
Yeah, that's it. Thanks.
The same is described in the article that JD linked...

Fortuna1

  • Full Member
  • ***
  • Posts: 164
Re: Custom DBGrid cell formatting
« Reply #9 on: February 04, 2011, 02:26:37 pm »
How   brush  if    ButtonStyle is                    cbsButtonColumn??

HOW change color  cbsButtonColumn in dbgrid   ??

Thanks
« Last Edit: February 08, 2011, 09:22:54 am by Fortuna1 »

darmawan

  • Newbie
  • Posts: 3
Re: Custom DBGrid cell formatting
« Reply #10 on: September 21, 2011, 11:24:08 am »
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..
Quote
procedure TCustomGrid.PrepareCanvas(aCol, aRow: Integer; aState: TGridDrawState);
var
  AColor: TColor;
  CurrentTextStyle: TTextStyle;
  IsSelected: boolean;
begin
  if DefaultDrawing then begin
    Canvas.Pen.Mode := pmCopy;
.
.
.

 

TinyPortal © 2005-2018