• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 510
  • Last Modified:

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

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
nzboss
Asked:
nzboss
3 Solutions
 
LMuadDIbCommented:
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
 
Ivanov_GCommented:
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
 
zork007Commented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Ivanov_GCommented:
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
 
Eddie ShipmanAll-around developerCommented:
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
 
nzbossAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now