Re:Controlling Cells in Grid
The OnDraw method could be something like this :
procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Longint;
Rect: TRect; State: TGridDrawState);
var
n : double;
C, R : longint;
S1, S2 : string;
begin
(* Save Row and Col parameters so they don't conflict with the grid's
Row and Col properties *)
C := Col;
R := Row;
with Sender as TStringGrid do
begin
Canvas.FillRect(Rect);
if Cells[C, R] <> '' then
begin
try
n := StrToFloat(Cells[C, R]);
if n < 0 then Canvas.Font.Color := clRed
else Canvas.Font.Color := clBlack;
finally ;
end;
end;
(* To wrap long lines *)
if Canvas.TextWidth(Cells[C, R]) > Rect.Right - Rect.Left then
begin
(* your own code to split text into two strings : S1 & S2 *)
Canvas.TextOut(Rect.Left + 2, Rect.Top, S1);
Canvas.TextOut(Rect.Left + 2. Rect.Top + TextHeight(S1), S2);
end else Canvas.TextOut(Rect.Left+2, Rect.Top+2, Cells[C, R]);
end;
end;
Greg Mockler
Pietermaritzburg, South Africa
greg...@iafrica.com
Quote
> Can anyone point me in the correct direction to make a cell in a grid
> appear in a different colour, if, for example the value is negative.
> The Data Source for this grid will be a table in the usual way.
> I think that I need OnDrawDataCell, so I think that I have to set
> DefaultDrawing to False, then supply my own code, but the code has me
> beaten. Does anyone have any samples?
> Also, is a multi-line cell possible? - i.e., if the text is too wide to
> fit in the column, display a second line.
> --
> Pete Clark