Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 443
  • Last Modified:

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...
0
komputer
Asked:
komputer
1 Solution
 
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
komputerAuthor Commented:
i think the problem is my database (ms sql server) doesnt hve recno property.
EddieShipman's solution is ok.

thaks guys...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now