Solved

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

Posted on 2004-09-23
6
495 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
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
delphi custom sort exception 6 111
Convert Jpg /PNG To GIF 5 102
Downloading email attachments 2 54
Delphi inherited method 6 37
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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now