简体   繁体   中英

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. 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.

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:

 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM