slyna
asked on
Different color in Stringgrid
How do i define a special color/font for each cell in a Stringgrid?
I want the text in a special cell to have a special color. If i click in it I want the text to become red.
I want the text in a special cell to have a special color. If i click in it I want the text to become red.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why I recommend the use of FW component :
Working with different fonts in a grid is not that easy to get a nice look. It will give you some headaches.
Of course if the code Fulvio is more then sufficient, don't hesitate to use it!
Regards, Zif.
Working with different fonts in a grid is not that easy to get a nice look. It will give you some headaches.
Of course if the code Fulvio is more then sufficient, don't hesitate to use it!
Regards, Zif.
ASKER
Wonderfull, just tell me how to check if a string is convertable to a float then you will get your points!
something like this:
if isvalidfloat(stringgrid2.c ells[2,4]) then
blaha
else
blah
something like this:
if isvalidfloat(stringgrid2.c
blaha
else
blah
ASKER
Mr fulvio_brasil
What the heck, you'll get your points anyway, but please answer the question above.
Thanks!
What the heck, you'll get your points anyway, but please answer the question above.
Thanks!
Hi slyna,
This code works very well, but, if you are in DELPHI IDE you will see a lot of debug's breaks about the except. Out of IDE, your app will work very well.
procedure TForm1.StringGrid1DrawCell (Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var gr: TStringGrid;
begin
gr := (Sender as TStringGrid);
try
gr.Canvas.Font.Color := clBlack;
StrToFloat(gr.Cells[ACol,A Row]);
except
gr.Canvas.Font.Color := clGray;
end;
if (ARow=gr.Row) AND (ACol=gr.Col)
then gr.Canvas.Font.Color := clRed;
gr.Canvas.FillRect(Rect);
gr.Canvas.TextOut(Rect.Lef t,Rect.Top ,gr.Cells[ ACol,ARow] );
end;
If you are using Delphi 4 or more, go to menu <tools>, <debuger options>, <language exception> tab and add to ignore 'EConvertError' and, until in IDE you won't have problems.
Fulvio.
This code works very well, but, if you are in DELPHI IDE you will see a lot of debug's breaks about the except. Out of IDE, your app will work very well.
procedure TForm1.StringGrid1DrawCell
Rect: TRect; State: TGridDrawState);
var gr: TStringGrid;
begin
gr := (Sender as TStringGrid);
try
gr.Canvas.Font.Color := clBlack;
StrToFloat(gr.Cells[ACol,A
except
gr.Canvas.Font.Color := clGray;
end;
if (ARow=gr.Row) AND (ACol=gr.Col)
then gr.Canvas.Font.Color := clRed;
gr.Canvas.FillRect(Rect);
gr.Canvas.TextOut(Rect.Lef
end;
If you are using Delphi 4 or more, go to menu <tools>, <debuger options>, <language exception> tab and add to ignore 'EConvertError' and, until in IDE you won't have problems.
Fulvio.
I would suggest to use a already made component (besides, why to reïnvent the wheel?). Here are some FW ones :
I've used the 3 first. Let me know if you want more info.
(tHyperGrid)
http://www.pablop.demon.co.uk/marley/thypergrid.htm
or
(tSuperGrid)
http://www.pablop.demon.co.uk/marley/tsupergrid.htm
(tAdvGrid)
http://www.tmssoftware.com/advgrid.htm
(tColorGrid)
http://www.csse.monash.edu.au/~vtran/
(tDataGrid)
http://www.ec-software.com/comppage.htm
etc..
Regards, Zif