Solved

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

Posted on 2004-09-23
6
501 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

726 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