Link to home
Start Free TrialLog in
Avatar of komputer
komputer

asked on

coloring dbgrid rows

hi,

i would like to color dbgrid rows if row count is odd, row color is red else blue.
i tried this,

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin

  if (query1.RecNo mod 2) = 1 then
    DBGrid1.Canvas.Brush.Color := clRed
  else
    DBGrid1.Canvas.Brush.Color := clBlue;

  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

but it doesnt work, cause query1.RecNo is always -1.

if you help me, i will pleased.

thanks...
Avatar of vadim_ti
vadim_ti

try instead of
query1.RecNo

DBGrid1.DataSource.DataSet.ActiveRecord
Avatar of komputer

ASKER

but i cant DBGrid1.DataSource.DataSet.ActiveRecord property,

given error message 'undeclared identifier: 'ActiveRecord'
Avatar of kretzschmar
>but it doesnt work, cause query1.RecNo is always -1.

after the open-method, or in the afteropen-event use

query1.fetchall;

this will edjust the recno-property

attention: this will cause a performance leak after open a huge dataset

meikl ;-)
to kretzschmar

i tried but it doesnt work. it stills return -1 as RecNo
well, in this case use additional the last-method like

query1.fetchall;
query1.last;

hope thats it

meikl ;-)
to kretzschmar again

i tried but it doesnt work. it still returns -1 as RecNo
well, then you can't use the recno for this
(the correct entry depends on the database u use)
I tested the following with both a TTable and TQuery dataset linked to the datasource. Both works fine.

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if Odd(DbGrid1.DataSource.DataSet.RecNo)
    then DBGRID1.Canvas.Brush.Color:= clBlue
    else DBGRID1.Canvas.Brush.Color:= clRed;
  DBGRID1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
  Invalidate;
end;

What dataset are you using? What type query?
ASKER CERTIFIED SOLUTION
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i think the problem is my database (ms sql server) doesnt hve recno property.
EddieShipman's solution is ok.

thaks guys...