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...
komputerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vadim_tiCommented:
try instead of
query1.RecNo

DBGrid1.DataSource.DataSet.ActiveRecord
0
komputerAuthor Commented:
but i cant DBGrid1.DataSource.DataSet.ActiveRecord property,

given error message 'undeclared identifier: 'ActiveRecord'
0
kretzschmarCommented:
>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 ;-)
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

komputerAuthor Commented:
to kretzschmar

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

query1.fetchall;
query1.last;

hope thats it

meikl ;-)
0
komputerAuthor Commented:
to kretzschmar again

i tried but it doesnt work. it still returns -1 as RecNo
0
kretzschmarCommented:
well, then you can't use the recno for this
(the correct entry depends on the database u use)
0
Pierre CorneliusCommented:
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?
0
Eddie ShipmanAll-around developerCommented:
Try this instead, it even works when you have datasets that don't have a RecNo property or it is always -1:
{originally posted to the Borland newsgroups by Sebastian Garde}

// Write an OnDrawColumnCell handler like this:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
 DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if ((((Rect.Top div (Rect.Bottom - Rect.Top)) mod 2) <> 1))
    and (not (gdSelected in State)) then
  begin
     DBGrid1.Canvas.Brush.Color := clRed;
  end
  else
  begin
     DBGrid1.Canvas.Brush.Color := clBlue;
  end;
 DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

// set the afterscroll for your dataset like this:
// I used a TADOQuery to test...
procedure TForm1.ADOQuery1AfterScroll(DataSet: TDataSet);
begin
  DBGrid1.Repaint;
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
komputerAuthor Commented:
i think the problem is my database (ms sql server) doesnt hve recno property.
EddieShipman's solution is ok.

thaks guys...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.