Page 1 of 1

Colorchange in TCRDBGrid

Posted: Tue 11 Jan 2005 12:08
by Thomas Speck
Hello,

how can I change every second row the Color of the Cells of a TCRDBGrid ?

I have made the following:

Code: Select all

procedure TForm.CRDBGridDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
  if (OraTable.RecNo mod 2)  1 then
  begin
    Canvas.Brush.Style := bsSolid;
    Canvas.Brush.Color := clBlue;
  end
  else
  begin
    Canvas.Brush.Style := bsSolid;
    Canvas.Brush.Color := clWhite;
  end;
  CRDBGrid.DefaultDrawColumnCell (Rect, DataCol, Column, State);
end;
This doesn't change anything in the Colors of the Cells of the Grid. How can I make it right ?

Posted: Tue 11 Jan 2005 14:21
by The_Rebel
maybe you should not tell delphi to override your drawed column by calling the default draw-routine after your code...

short C++ Sample of my drawing routine:

Code: Select all

  
  TCRDBGrid *My = dynamic_cast (Sender);

  if (My) {
      TCanvas *pCanvas = My->Canvas;
      //if (Canvas) ...
      pCanvas->Font->Color = clBlack;
      pCanvas->Brush->Color = clNavy;

      pCanvas->FillRect( Rect );
      pCanvas->TextOutA( Ract->Left, Rect->Top, ValueToShow.c_str() );
  }
  My = 0;

Posted: Tue 11 Jan 2005 14:45
by Alex
You need to change Brush property of the TCRDBGrid object not the form
object, so you should change your code in this way:

Code: Select all

 procedure TForm.CRDBGridDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
 begin
  with CRDBGrid.Canvas.Brush do
    if (OraTable.RecNo mod 2)  1 then
      begin
        Style := bsSolid;
        Color := clBlue;
      end
      else
      begin
        Style := bsSolid;
        Color := clWhite;
      end;
  CRDBGrid.DefaultDrawColumnCell (Rect, DataCol, Column, State);
 end;

Posted: Tue 11 Jan 2005 14:50
by Guest
Thank you for your Post, but your Solution doesn't work. After uncommenting DefaultDrawColumnCell nothing happens.

I will debug the Source tomorrow and will post my Solution then.

Posted: Tue 11 Jan 2005 14:52
by The_Rebel
sorry for my confusing post... (my delphi time is long ago)

i can also use the sender object to make your function accessable for different sender's, ther must be a dynamic_cast in delphi...

ps.: also the row attribute of the grid can be used for your mod 2

Posted: Tue 11 Jan 2005 15:00
by Thomas Speck
It works now, I have forgotten to Set "DefaultDrawing" in Objectinspector to False.

Thank you for your Help !!