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

时间:2019-01-29 20:23:42

标签: lazarus delphi

我有一个包含两个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;                      

1 个答案:

答案 0 :(得分:1)

在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;