Am having a string grid( TStringGrid
) with 2 column and 1 row ( Property: ColCount = 2 & Rowcount = 1
.
Code for OnDrawCell
Event:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
Parametertext : string;
begin
case ACol of
0 : Parametertext := 'Test';
1 : Parametertext := 'Test1';
end;
stringgrid1.Brush.Color := clBtnFace;
stringgrid1.Font.Color := clWindowText;
stringgrid1.Canvas.FillRect(Rect);
DrawText(stringgrid1.Canvas.Handle, PChar(parameterText), -1, Rect,
DT_SINGLELINE);
end;
When I run the application, I get the below output:
Question:
When I try to get the text using StringGrid1.Cells[0,0]
, StringGrid1.Cells[1,0]
,
I except "Test" & "Test1" but it always gives a empty string"".
How can I get the text from string grid using StringGrid.Cells[aCol,aRow]
?
You are generating the text to draw it, but not storing it. You also need to set the stringGrid.Cells value, probably not in the OnDrawCell event, though.
Think about your variable Parametertext. It is a local variable destroyed on exit. Nowhere do you save it anywhere else. So why would you expect it to magically appear in the cells property?
To do what you are asking, you need to actually store the string values in the Cells
property, not generate them dynamically in the OnDrawCell
event:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
Parametertext : string;
begin
Parametertext := StringGrid1.Cells[ACol, ARow];
StringGrid1.Brush.Color := clBtnFace;
StringGrid1.Font.Color := clWindowText;
StringGrid1.Canvas.FillRect(Rect);
DrawText(StringGrid1.Canvas.Handle, PChar(ParameterText), Length(ParameterText), Rect, DT_SINGLELINE);
end;
...
StringGrid1.Cells[0, 0] := 'Test';
StringGrid1.Cells[1, 0] := 'Test1';
If you are not going to use the Cells
property to store strings, you may as well have just used TDrawGrid
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.