Change color of a grid row

Using Delphi 3 it is quite easy to change the color of a column but I can't see how to changed the color of a row (or even just a single cell, for that matter) . My main interest is a dbgrid but I guess the same principle applies to a stringgrid.

For example I have a customer DBgrid that I would like to highlight all customers over 30 days due, in the color red
clyde99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
hi,
i give you 2 examples of drawing the grids(yes i have the samples in stringgrids but also applies to dbgrids :-)

example 1:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  FocusedRect : TRect;
begin
  FocusedRect := Rect;
  StringGrid1.Canvas.Font.Color  := clBlack;
  StringGrid1.Canvas.Pen.Color := clRed;
  StringGrid1.Canvas.Pen.Width := 1;
  if ( gdFocused in State ) then begin
    StringGrid1.Canvas.Font.Style  := [fsBold];
    StringGrid1.Canvas.Font.Color  := clBlue;
    StringGrid1.Canvas.Brush.Color  := clRed;
  end
else
    StringGrid1.Canvas.Font.Style  := [];
  InflateRect(FocusedRect, -2, -2);
  StringGrid1.Canvas.FillRect(FocusedRect);
   if ( gdSelected in State ) then
    StringGrid1.Canvas.TextOut(Rect.Left+8, Rect.Top+2, StringGrid1.Cells[ACol,ARow])   else
    StringGrid1.Canvas.TextOut(Rect.Left+2, Rect.Top+2, StringGrid1.Cells[ACol,ARow])
 end;


example 2:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
 checked: boolean;
 oldbrush: integer;
 oldpen: tpen;
begin
 with stringgrid1.canvas,rect do
 begin
  oldbrush:=brush.color;
  brush.color:=clyellow;
  oldpen:=pen;
  pen.Width:=2;
  pen.color:=clred;
  moveto(left+1,top+2);
  polygon([point(left,top),
           point(left,bottom),
           point(right,bottom),
           point(right,top),
           point(left,top)]);
  checked:=boolean(stringgrid1.Objects[acol,arow]);
  if checked then
  begin
  brush.Color := clblue;
  polygon([point(left,top),
           point(left,bottom),
           point(right,bottom),
           point(right,top),
           point(left,top)]);
  end;
  brush.color:=oldbrush;
  textout(left+17,top+4,stringgrid1.cells[acol,arow]);
  pen:=oldpen;
 end;
end;

procedure TForm1.StringGrid1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
 r,c: integer;
 checked: boolean;
begin
 with stringgrid1 do
 begin
  mousetocell(x,y,c,r);
  checked:=(longint(objects[c,r])<>0);
  if checked then objects[c,r]:=pointer(0) else objects[c,r]:=pointer(1);
 end;
 end;


should give some ideas
Regards Barry
0
intheCommented:
btw.
for example2 set the defaultdrawing to false in the object inspector.
0
clyde99Author Commented:
The above code for example 1 only seems to work when I select a cell with the mouse. How do I get the the required cells/rows to change color when the grid is initially displayed?
0
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.

intheCommented:
hi,
try this :
I created a form, dropped a TTable component it, and pointed it to the EMPLOYEE.DB database in the DBDEMOS database. I dropped a Datasource and DBGrid on the form so that it showed on the form.
then set the code of the form to be like:


var
  Form1: TForm1;
    SOMEDate : TDate;
implementation

{$R *.DFM}

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  holdColor: TColor;
begin
  holdColor := DBGrid1.Canvas.Brush.Color;
  if Column.FieldName = 'HireDate' then
     if (Column.Field.AsDateTime < SOMEDate )then begin
      DBGrid1.Canvas.Brush.Color := clred;
      DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
      DBGrid1.Canvas.Brush.Color := holdColor;
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 SOMEDate := Strtodate('01/06/1992');
end;


is that better?
Regards Barry
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
clyde99Author Commented:
Barry,

Thankyou.

That was EXACTLY what I was trying to do

Clyde
0
intheCommented:
good to hear &     :-))
merry xmas
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.