c# - 如何从 C# 中同一项目下的不同窗口窗体访问不同组件?

标签 c# mysql database

例如,在表单 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/

相关文章:

MySQL:根据不同的列减去不同的行

php - 根据同一表中的其他值标记/更新值

c# - 最近的 Twitter API 1.1 的 Twitterizer 版本

Mysql:显示不包含另一个表中的文本模式的查询结果

c# - 发布新版 ASP.NET 网站 - 最佳实践

PHP 和 MySQL : Find out which file or query is causing heavy CPU load

database - NoSQL 值得使用

java - 如何在 MySQL 中创建临时过程?

c# - 模拟鼠标光标在 c# 中在两个坐标之间移动

c# - 查看 ItemsControl 内的注入(inject)