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?
source to share
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.
source to share
you should try also free 3d party solution and extend already many DBGrid like those provided by Jedi project
source to share