Link to home
Start Free TrialLog in
Avatar of clyde99
clyde99

asked on

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
Avatar of inthe
inthe

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
btw.
for example2 set the defaultdrawing to false in the object inspector.
Avatar of clyde99

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of clyde99

ASKER

Barry,

Thankyou.

That was EXACTLY what I was trying to do

Clyde
good to hear &     :-))
merry xmas