Extending DBGrid with some row colors

I want to extend the functionality of the DbGrid to add colors to odd and even rows. So I wrote this

procedure TGridx.DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);
var
  row : Integer;
begin
   inherited;
  row := Self.DataSource.DataSet.RecNo;
  if (row mod 2 = 0) then
    Self.Canvas.Brush.Color := FColor1  //some color
  else
    Self.Canvas.Brush.Color := FColor2; //some color

end;

      

What am I doing wrong?

+3


source to share


4 answers


The event you want is called DBGridDrawColumnCell

and you need to decide whether to enable or disable the DefaultDrawing property, and handle the way the DBGridDrawColumnCell handles accordingly. For your case, you just set the colors, but leave DefaultDrawing true and don't do any other canvas.Text or GDI drawing.



A recent question I asked here showed that in later versions of Delphi (2010, Xe, Xe2) you also sometimes need to call Canvas.Refresh

for TDBGRID and TListView when canvas properties change in ownerdraw events, but this is not the case for delphi 7.

+5


source


you should try also free 3d party solution and extend already many DBGrid like those provided by Jedi project



+1


source


Opc0de, maybe you should override the "DrawCell" method, but "DrawCellBackground"?

0


source


Try painting the cell also after defining the brush color:

Self.Canvas.FillRect(ARect);

      

0


source







All Articles