Link to home
Start Free TrialLog in
Avatar of rperies
rperies

asked on

Changing the colour of CELLS/TEXT in a stringgrid at Runtime...

I have a StringGrid that is linked to a two dimensional array of records and I am trying to change the colour of the text in a stringgrid in order to correspond to a boolean variable field in the array. If the value is false, then the text should be green, otherwise the text should be red (If the value of the field is true). On startup, the text in the cells within the stringgrid is black. On formactivate, I want them to be green, and then, whenever the array is updated, I want them to turn red (when the value in the boolean field, lets call it BOOKED, becomes true)
If the colour of the CELLS, rather than the TEXT can be changed, then I wouldn't mind that either. In fact, I would prefer it.

Thanks

rperies
Avatar of nestorua
nestorua

HI,
Sorry, but I don't know if I sent an answer.
Sincerely,
Nestorua.
Did I sent the answer or not?
if you wish to change the font color use nect tip :

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
  StringGrid1.Canvas.FillRect(Rect);
  if ARow = 1 then  {Use ACol for column or use both for a cell}
  begin
    StringGrid1.Canvas.Font.Color := clBlue;
    StringGrid1.Canvas.Font.Name := 'Tahoma' ;
    StringGrid1.Canvas.Font.Style := StringGrid1.Canvas.Font.Style + [fsBold];
  end;
  DrawText(StringGrid1.Canvas.Handle, PChar(StringGrid1.Cells[ACol, ARow]), -1,
                     Rect, DT_SINGLELINE or DT_VCENTER or DT_LEFT);
end;
For cell color use StringGrid1.Canvas.Brush.Color := clBlue;
HI,
I don?t know why but my answer
Wasn?t sent.
So I try to answer the question as a comment.
The solution of the problem has the following plan:
1.Create a new component from TStringGrid
(let it call TPSIStringGrid).
2.Declare a new TEvent ? TPSIDrawCell.
3.Declare a field  FOnPSIDrawCell of type
TPSIDrawCell.
4.Declare a property OnPSIDrawCell which reads
FOnPSIDrawCell and writes FOnPSIDrawCell.
5.Declare a procedure PSIDrawCell which checks
if FOnPSIDrawCell<>NIL (Assigned).
6.Override the procedure DrawCell in such way:
first execute PSIDrawCell then inherited.
7.Install the TPSIStringGrid on the Pallette.
That?s all.
Now you can change Font and Brush Colors in
OnPSIDrawCell whatever the condition you want.
Sincerely,
Nestorua.
Avatar of rperies

ASKER

Sorry, I appreciate the effort but unfortunately this answer seems far too complicated.
Avatar of rperies

ASKER

Ginsonic - I found that your code worked initially, but I can't assign it to a different procedure, such as a buttonclick. The stringgrid should continue to change colour after the record is updated. Can it be done independently of DrawCell?

rperies
ASKER CERTIFIED SOLUTION
Avatar of ginsonic
ginsonic
Flag of Romania image

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
This sample show how can refresh the cell at update.
HI, rperies,
What a step of my answer is too complicated for you.
I think you asked not only to know the answer but to know
how to solve problems of the kind and to teach youself
new things.
Sincerely,
Nestorua.