komputer
asked on
coloring stringgrid
hi,
i am using stringgrid and i would like to color the rows so onDrawCell event i wrote
mySGrid.Brush.Color := clred;
but it doesnt work.
if you help me, i will be pleased.
thanks.
i am using stringgrid and i would like to color the rows so onDrawCell event i wrote
mySGrid.Brush.Color := clred;
but it doesnt work.
if you help me, i will be pleased.
thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
After you set DefaultDrawing to False then try this code :
procedure TForm1.StringGrid1DrawCell
Rect: TRect; State: TGridDrawState);
begin
// set the font Bold and Red
StringGrid1.Canvas.Font.Co
StringGrid1.Canvas.Font.St
// draw the text in the cell
StringGrid1.Canvas.TextOut
StringGrid1.Cells[ACol, ARow]);
end;
procedure TForm1.StringGrid1DrawCell (Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
const //define your color here.
clPaleGreen = TColor($CCFFCC);
clPaleRed = TColor($CCCCFF);
begin
//Does the cell have the focus you have to paint it with other colors
if (gdFocused in State) then
begin
StringGrid1.Canvas.Brush.C olor := clBlack;
StringGrid1.Canvas.Font.Co lor := clWhite;
end
else //Does the cell have NOT the focus you can use
//your personal colors here
if ACol = 2 //the second Column should be
{//green, the other cells red } then
StringGrid1.Canvas.Brush.c olor := clPaleGreen
else
StringGrid1.canvas.brush.C olor := clPaleRed;
//Now Paint the cells, but only, if the cell isn't the Title- Row/Column
//This of course depends whether you have title-Row/Columns or not.
if (ACol > 0) and (ARow > 0) then
begin
//Painting the Background
StringGrid1.canvas.fillRec t(Rect);
//Painting the Text. Here you can improve the code with
// using alignment and so on.
StringGrid1.canvas.TextOut (Rect.Left , Rect.Top, StringGrid1.Cells[ACol, ARow]);
end;
end;
If you want to colorize your cells depending on values in the cells you can replace the 3 lines (if Acol = 2 ......) with something like this
if StringGrid1.Cells[ACol, ARow] = 'highlight it' then
StringGrid1.Canvas.Brush.c olor := clPalered
else
StringGrid1.canvas.brush.C olor := clwhite;
Rect: TRect; State: TGridDrawState);
const //define your color here.
clPaleGreen = TColor($CCFFCC);
clPaleRed = TColor($CCCCFF);
begin
//Does the cell have the focus you have to paint it with other colors
if (gdFocused in State) then
begin
StringGrid1.Canvas.Brush.C
StringGrid1.Canvas.Font.Co
end
else //Does the cell have NOT the focus you can use
//your personal colors here
if ACol = 2 //the second Column should be
{//green, the other cells red } then
StringGrid1.Canvas.Brush.c
else
StringGrid1.canvas.brush.C
//Now Paint the cells, but only, if the cell isn't the Title- Row/Column
//This of course depends whether you have title-Row/Columns or not.
if (ACol > 0) and (ARow > 0) then
begin
//Painting the Background
StringGrid1.canvas.fillRec
//Painting the Text. Here you can improve the code with
// using alignment and so on.
StringGrid1.canvas.TextOut
end;
end;
If you want to colorize your cells depending on values in the cells you can replace the 3 lines (if Acol = 2 ......) with something like this
if StringGrid1.Cells[ACol, ARow] = 'highlight it' then
StringGrid1.Canvas.Brush.c
else
StringGrid1.canvas.brush.C
The StringGrid has propery DefaultDrawing.
Set DefaultDrawing = False - for owner draw