将数据从一个表单传递到另一个表单

LMi*_*Pop -4 c# winforms

我的一个项目中有两种形式.在form1中我有一个dataGridView和In form2我有4个TextBoxes.在Form1中,我想使用CellMouseClick事件从datagridview获取变量值,然后将其传递给Form2中的TextBox

我试过这个.

form1#它给我一个错误

public form(int id)
{
    int x;
    x = dataGridView1.CurrentRow.Cells[0].Value.ToString();
}
Run Code Online (Sandbox Code Playgroud)

以及我想在form2中做的事情

Gra*_*mas 7

使用构造函数,您可以构造具有给定构造先决条件的类型.

如果这意味着一个整数,那么就这样吧:

public MyForm(int id) {
  SomeIdProperty = id;
}
Run Code Online (Sandbox Code Playgroud)

而不是var form = new MyForm();,做:

var form = new MyForm(idOfTheRelevantThing);
Run Code Online (Sandbox Code Playgroud)

然后展示它.