3

我之前已经看到这个问题是另一个问题的一部分,所以知道它不能只是我......如果我打开一个新的 FireMonkey2 HD 应用程序,添加一个 TButton 和 TStringGrid,然后将其添加到单击事件的按钮中,当我单击按钮时,网格中什么也没有发生!

procedure TForm33.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  for i:= 0 to 6 do
   begin
     StringGrid1.Cells[0,i] := 'Row:' + IntToStr(i);
   end;
   stringgrid1.UpdateColumns;
   stringgrid1.SetFocus;
 end;    

有任何想法吗 ?PS 我也尝试过使用 TStringGrid.OnGetValue,但它仍然不会在 StringGrid 中显示任何内容。

进一步查看 TStringGrid 源代码后,C 为 nil,因此从未设置单元格。

procedure TStringGrid.SetValue(Col, Row: Integer; const Value: TValue);
var
  C: TColumn;
begin
  C := Columns[Col];
  if Assigned(C) then
  begin
    C.UpdateRowCount(RowCount);
    C.SetCells(Row, Value);
  end;

我似乎在“处女”StringGrid 中没有列,那么如何添加它们?有一个 r+w RowCount 属性,但 ColCount 是只读的...

4

1 回答 1

5

您必须至少添加一列TStringGrid才能向单元格添加数据,您可以在运行时执行此操作

StringGrid1.AddObject(TStringColumn.Create(Self)); 

或在设计时使用项目设计器

在此处输入图像描述

于 2012-11-09T14:41:37.860 回答