使用变量作为单元格数据将行添加到 StringGrid?

Add rows to StringGrid with variables as cell data?

我有一个包含两个 TEdit 组件的表单。在另一种形式上,我希望使用来自两个 TEdits 的数据将一行添加到 TStringGrid。我该怎么做?

这是我目前拥有的:

procedure TSecondForm.StartButtonClick(Sender: TObject);
begin
  string1 := Edit1.Text;
  string2 := Edit2.Text;

  MainForm.StringGrid1.RowCount := MainForm.StringGrid1.RowCount + 1; // this adds the rows, but I don't know how to make it so that the two variables are inputed into two seperate cells
end;                      

在Delphi和FreePascal/Lazarus中,可以在RowCount递增后使用TStringGrid.Cells属性,例如:

procedure TSecondForm.StartButtonClick(Sender: TObject);
var
  string1, string2: string;
  row: integer;
begin
  string1 := Edit1.Text;
  string2 := Edit2.Text;

  row := MainForm.StringGrid1.RowCount;
  MainForm.StringGrid1.RowCount := row + 1;
  MainForm.StringGrid1.Cells[0, row] := string1;
  MainForm.StringGrid1.Cells[1, row] := string2;
end;

仅在 FreePascal/Lazarus 中,您可以交替使用 TStringGrid.InsertRowWithValues() 方法:

procedure TSecondForm.StartButtonClick(Sender: TObject);
var
  string1, string2: string;
begin
  string1 := Edit1.Text;
  string2 := Edit2.Text;

  MainForm.StringGrid1.InsertRowWithValues(MainForm.StringGrid1.RowCount, [string1, string2]);
end;