Jump to content


Photo

Painting issue with custom cell coloring


  • Please log in to reply
6 replies to this topic

#1 yeohray

yeohray
  • Members
  • 24 posts

Posted 26 June 2007 - 07:50 PM

I am using a TNextDBGrid, which is not painting correctly when I retrieve the value of a cell to determine how the cell should be colored. E.g.

procedure TForm1.grdMainCellColoring(Sender: TObject; ACol, ARow: Integer;
var CellColor, GridColor: TColor; CellState: TCellState);
var
CellValue: string;
begin
CellValue := grdMain.Cells[ACol, ARow]; // removing this row solves the painting issue

if (ARow mod 20 = 0) and (ACol = 2) then
CellColor := clSilver;
end;


How can I retrieve the cell value and have TNextDBGrid paint itself correctly? Thanks.


Ray Mond

#2 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 26 June 2007 - 07:59 PM

Hello Ray Mond,

I recommend using column's Field property (TField). As you probably know, you may read AsString property of Field.

You may find similar use from sample project in \Demos\Next DBGrid\ADO\Demo 1

Here is a small snapshot:

CODE
var
  R: TRect;
  Field: TField;
begin
  if ACol = 1 then
  begin
    Field := NextDBGrid1.Columns[ACol].Field;
    if Assigned(Field) and not Field.IsNull then
    begin
      R := CellRect;
      InflateRect(R, -3, -2);
      NextDBGrid1.Canvas.Pen.Color := clGrayText;
      NextDBGrid1.Canvas.Brush.Color := Field.AsInteger;
      NextDBGrid1.Canvas.Rectangle(R);
    end;
  end;
end;


When DBGrid draw cells, it go from first visible record on grid to the last visible record in grid. In this process, ActiveRecord is changed in internal buffer (DataLink), and every Field from DataSet load data from current record.

I hope that it helps smile.gif

regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#3 yeohray

yeohray
  • Members
  • 24 posts

Posted 28 June 2007 - 06:36 PM

Thanks, that did it.

Ray Mond

#4 Aage J.

Aage J.
  • Members
  • 13 posts

Posted 27 July 2007 - 10:07 PM

QUOTE (Boki (Berg) @ Jun 26 2007, 08:59 PM) <{POST_SNAPBACK}>
Hello Ray Mond,

I recommend using column's Field property (TField). As you probably know, you may read AsString property of Field.

You may find similar use from sample project in \Demos\Next DBGrid\ADO\Demo 1

Here is a small snapshot:

<snip>


I'm trying to implement something like this (the whole row given another background color depending on one field). This used to work ok with previous versions of the components, using OnCellColoring and OnCellFormatting.
However, it seems to me that the OnCustomDrawCell does not get triggered! I've placed a breakpoint on the first statement, but it doesn't break. Data displays properly in the grid, but coloring fails.

Could this be an installation problem? I had some problems with the install, and I've seen other problems with the grid.


Regards,
Aage J.

#5 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 27 July 2007 - 10:17 PM

Hello Aage,

Maybe you have forgot to set DrawingOptions property of Column to other value than doNormal?

Also, here is a one small example how to colorize rows from Field:

CODE
procedure TForm1.NextDBGrid1CellColoring(Sender: TObject; ACol,
  ARow: Integer; var CellColor, GridColor: TColor; CellState: TCellState);
var
  Field: TField;
begin
  Field := NextDBGrid1.Columns[<ColumnIndex>].Field;
  if Assigned(Field) and not Field.IsNull then
  begin
    CellColor := Field.AsInteger;
  end;
end;


Best regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#6 VrEx

VrEx
  • Members
  • 5 posts

Posted 22 May 2009 - 04:12 PM

Hi, in CellColoring event , when you check CellState it always has csSelected in it. And SelectedRow property is always changes to current drawing row. How can distinguish selected row?

#7 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 22 May 2009 - 04:43 PM

Hi VrEx,

Next code work very fine on my sample project:

CODE
  if csSelected in CellState then CellColor := clRed;


Can you please tell me in more details what you need, or send me a small demo project to I test it. Thank you.

Best regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users