Solved

DBGrid and row colour (White/Light Grey/White/Light Grey etc)

Posted on 2004-09-23
6
500 Views
Last Modified: 2010-04-05
Hi experts,

Can anyone please tell me how I can go about having each row of my result set a different colour....

That is have one row white and the next a light gery followed by white, followed by light grey....etc....

Is this possible?  if so how?

Thanks, look forward to your responses.
0
Comment
Question by:nzboss
[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
6 Comments
 
LVL 4

Assisted Solution

by:LMuadDIb
LMuadDIb earned 20 total points
ID: 12139102
On your DBGrid1DrawColumnCell event put the following code:
Just change your table name or grid etc....


procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
      var Color : TColor;
begin
  if not(gdSelected in state) then
  begin
    if (ADOTable1.RecNo mod 2)=1 then
      Color := RGB(225, 225, 225)   //a color
    else
      Color := RGB(255, 255, 255); //another color
    Dbgrid1.Canvas.Brush.Color := Color;
    Dbgrid1.Canvas.FillRect(Rect);
  end;
  dbGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
0
 
LVL 12

Assisted Solution

by:Ivanov_G
Ivanov_G earned 30 total points
ID: 12140521
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if ((ADOTable1.RecNo mod 2) = 0) then
    dbGrid1.Canvas.Brush.Color := clBlue
  else
    dbGrid1.Canvas.Brush.Color := clRed;
  dbGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
0
 

Expert Comment

by:zork007
ID: 12141739
Hi experts,

Thanks for your responses, so far I'm uinga  bit of both, LMuadDIb I liked the light grey color on yours so am using that, unfortunetely yours seems to miss coloring some rows...not ure why.

Ivanov G, yours seems to colour every row row in the correct colour so that is great, howevere, when I click on a row it does seem to want to highlight it...

I'll have a little play around but if you gusy know why that would be great.
0
Independent Software Vendors: 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!

 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12141943
The problems codes because one of your colors is White. When you select a cell, the font becomes white... on white background...

If you have for example clGray and clSilver, it is OK...
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 75 total points
ID: 12147945
Actually this works better as it will also work on datasets WITHOUT a RecNo property:

Change the color to your desired color...

Try this: {originally posted by Sebastian Garde}

// Write an OnDrawColumnCell handler like this:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  Grid: TDBGrid;
begin
  Grid := TDBGrid(Sender);
  if ((((Rect.Top div (Rect.Bottom - Rect.Top)) mod 2) <> 1))
     and (not (gdSelected in State)) then
  begin
    Grid.Canvas.Brush.Color :=clSilver;
  end
  else
  begin
    if (gdSelected in State) then
    begin
      Grid.Canvas.Font.Color  := clWindowText;
      Grid.Canvas.Brush.Color := clGray;
    end;
  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:nzboss
ID: 12175537
Thanks everyone for your help.  I will give everyone a slice of the pie but EddieShipman gets most of the points as if worked great.  The others helped assist with the correct colours and Ivanov_G gave another solution.

Thanks heaps people!
0

Featured Post

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Delphi: ForceDirectory plain function 7 84
Drag & Drop... Data from one grid to another 2 54
Multi-layered image in FireMonkey 9 93
Firemonkey Queue list 1 51
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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