?
Solved

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

Posted on 2004-09-23
6
Medium Priority
?
502 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 80 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 120 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 300 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

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.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 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