Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending DBGrid with some row colors

I want to extend DbGrid functionality to add colors on 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 i am doing wrong ?

like image 622
opc0de Avatar asked Jan 19 '26 07:01

opc0de


1 Answers

The event you want is called DBGridDrawColumnCell, and you need to decide whether to turn the DefaultDrawing property on or off, and the way you handle DBGridDrawColumnCell changes 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 Delphi versions (2010,Xe,Xe2) you ALSO sometimes need to call Canvas.Refresh for both TDBGRID and TListView, when changing canvas properties in ownerdraw events but that doesn't apply to delphi 7.

like image 70
Warren P Avatar answered Jan 21 '26 22:01

Warren P