Board index » delphi » DBGrid - Changing Colour Of Cell

DBGrid - Changing Colour Of Cell

Help !

I want to change the colour of the first cell/record field on my grid when
another field matches a criteria. I have used the following code and it
works EXCEPT it changes the next record colour and not the one i want ???
Please help.

procedure TEquipForm.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
   If MtsysDM.Equip.FieldByName('Critical').AsString='Yes' then
      begin
         DBGrid1.Columns[0].Color:=clRed;
   end;
   If MtsysDM.Equip.FieldByName('Critical').AsString<>'Yes' then
     Begin
         DBGrid1.Columns[0].Color:=clWindow;
   end;
end;

Regards

Tim

 

Re:DBGrid - Changing Colour Of Cell


Quote
In article <7ihg1k$2...@forums.borland.com>, Spooky wrote:
> I want to change the colour of the first cell/record field on my grid when
> another field matches a criteria. I have used the following code and it
> works EXCEPT it changes the next record colour and not the one i want ???
> Please help.

The DrawColumnCell event is called for every cell displayed in the grid, so
you should only be making changes when called for the first column.

Try this:

  procedure TEquipForm.DBGrid1DrawColumnCell(Sender: TObject;
    const Rect: TRect; DataCol: Integer; Column: TColumn;
    State: TGridDrawState);
  const clrs: array[boolean] of TColor = (clWindow,clRed);
  var isRed: boolean;
  begin
    if Column.Index = 0 then begin
      isRed := (MtsysDM.Equip.FieldByName('Critical').AsString='Yes');
      DBGrid1.Canvas.Brush.Color:=clrs[isRed];
    end;
    DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
  end;

If you want to change the color of the text, use DBGrid1.Canvas.Font.Color

 Mike Orriss (TeamB)
 (No e-mail replies, please, unless explicitly requested!)

Other Threads