如何动态地将值添加到delphi中的TStringGrid的单元格?

时间:2016-04-09 13:01:47

标签: delphi

proedure TForm5.ButtonClick(Sender:TObject);
var
  I, J: Integer;
const
  MyArray: array [1..5] of string = ('Siva', 'jindal', 'ram', 'kesu', 'Srinu ');
begin
  StringGrid1.RowCount := StringGrid1.RowCount + 1;
  for J := 1 to length(MyArray);
  for I := 0 to
    StringGrid1.Cells[I,StringGrid1.RowCount-1] := MyArray[J];

  StringGrid1.Row := StringGrid.RowCount-1;
end;

在此我能够动态创建行但无法将数组的值插入单元格中。

1 个答案:

答案 0 :(得分:3)

尝试更像这样的东西:

procedure TForm5.ButtonClick(Sender: TObject);
const
  MyArray: array[1..5] of string = ('Siva', 'jindal', 'ram', 'kesu', 'Srinu ');
var
  I, Row: Integer;
begin
  Row := StringGrid1.RowCount;
  StringGrid1.RowCount := Row + 1;
  for I := Low(MyArray) to High(MyArray) do
    StringGrid1.Cells[I-1, Row] := MyArray[I];
  StringGrid1.Row := Row;
end;