[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

coloring dbgrid rows

Posted on 2004-11-02
10
Medium Priority
?
429 Views
Last Modified: 2010-04-05
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
Comment
Question by:komputer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12471127
try instead of
query1.RecNo

DBGrid1.DataSource.DataSet.ActiveRecord
0
 

Author Comment

by:komputer
ID: 12471180
but i cant DBGrid1.DataSource.DataSet.ActiveRecord property,

given error message 'undeclared identifier: 'ActiveRecord'
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12471311
>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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:komputer
ID: 12471402
to kretzschmar

i tried but it doesnt work. it stills return -1 as RecNo
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12471433
well, in this case use additional the last-method like

query1.fetchall;
query1.last;

hope thats it

meikl ;-)
0
 

Author Comment

by:komputer
ID: 12472052
to kretzschmar again

i tried but it doesnt work. it still returns -1 as RecNo
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12472125
well, then you can't use the recno for this
(the correct entry depends on the database u use)
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 12472239
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
 
LVL 26

Accepted Solution

by:
EddieShipman earned 200 total points
ID: 12476377
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
 

Author Comment

by:komputer
ID: 12491323
i think the problem is my database (ms sql server) doesnt hve recno property.
EddieShipman's solution is ok.

thaks guys...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question