例如,在表单 4 中,我有一个数据 GridView ,当我从数据 GridView 中选择数据并单击更新时.. 会弹出表单 5,其中包含各种空文本字段。现在我想确保这些文本字段填充了我在数据 GridView 中选择的数据。我怎样才能做到这一点?
假设我在数据 GridView 中选择了第四行,即
id = 4
name = name
address = address
在表单 5 上,我有 3 个文本框,即当表单加载时,如何使这些文本框自动填充我在表单 4 的数据 GridView 中选择的数据。
最佳答案
可能最好的方法是为表单编写自定义构造函数,并将数据传递给它。像这样的事情:
public DisplayForm(int id, string name, string address)
{
InitializeComponent();
//Take the data that you passed to the constructor, and use it to update the text boxes:
txtID.Text = id.ToString();
txtName.Text = name;
txtAddress.Text = address;
}
然后使用您刚刚编写的构造函数调用它:
DisplayForm display = new DisplayForm(4, name, address);
您甚至可以创建一个将 DataGridViewRow 作为参数的容器:
public DisplayForm(DataGridViewRow row)
{
InitializeComponent();
//Take the data that you passed to the constructor, and use it to update the text boxes:
txtID.Text = row.Cells[0].Value.ToString();
txtName.Text = row.Cells[1].Value.ToString();
txtAddress.Text = row.Cells[2].Value.ToString();
}
关于c# - 如何从 C# 中同一项目下的不同窗口窗体访问不同组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45829207/