[英]How can i access the different component from different windows form which are under same project in c#?
For example in form 4 i have a data grid view and when i select a data from data grid view and click on update.. form 5 pops up with various empty text fields. 例如,在表单4中,我有一个数据网格视图,当我从数据网格视图中选择一个数据并单击更新时。表单5弹出带有各种空文本字段。 Now i wanna make sure those text fields are filled up with my data that i selected in data grid view. 现在,我要确保这些文本字段填充了我在数据网格视图中选择的数据。 How can i make that happen? 我怎样才能做到这一点?
say i selected 4th row in my data grid view which is 说我在数据网格视图中选择了第四行
id = 4
name = name
address = address
on form 5 i have 3 text box ie so when form load how can i make those text box auto-filled with the data i selected in data grid view in form 4. 在表格5上,我有3个文本框,即在加载表格时如何使那些文本框自动填充在表格4的数据网格视图中选择的数据。
Probably the best way is to write a custom constructor for your form, and pass the data to it. 最好的方法可能是为表单编写一个自定义构造函数,然后将数据传递给它。 Something like this: 像这样:
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;
}
You then call it using the constructor that you just wrote: 然后,使用刚刚编写的构造函数调用它:
DisplayForm display = new DisplayForm(4, name, address);
You could even create one that takes a DataGridViewRow as an argument: 您甚至可以创建一个以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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.