Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 434
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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