简体   繁体   中英

How can I pass data of datagridview to textboxes in other Form?

Datagridview is located in Form2, TextBoxes in Form1.

Call the Form 2 from Form1 with Show(); where is located dataGridView and then pass this information to textboxes in Form1.

Code Sample in Form2 :

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    Form1 exportar = new Form1();
    exportar.textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
    exportar.comboBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();
    exportar.textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString();
    exportar.textBox3.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
    exportar.textBox4.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].Value.ToString();
    exportar.dateTimePicker1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[5].Value.ToString();
    exportar.dateTimePicker2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[6].Value.ToString();
    exportar.textBox7.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[7].Value.ToString();
    exportar.textBox8.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[8].Value.ToString();
    exportar.textBox9.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[9].Value.ToString();
    exportar.textBox10.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[11].Value.ToString();
}

This did not work, but when I place exportar.Show() passed the information. The problem is that doubles the Form1.

You need a reference of Form1 in Form2. You can pass it in the constructor of Form2

private Form1 _form1;

public Form2 (Form1 form1)
{
    _form1 = form1;
}

You create and open Form2 like this from within Form1:

var form2 = new Form2(this);
form2.ShowDialog(this);

In order to be able to access the controls of the other form, you must change their Modifer to Internal in the properties window.

then you can set the values like this:

var row = dataGridView1.CurrentRow; // This is "the row".
                                    // No detour through the index is necessary.
_form1.textBox1.Text = row.Cells[0].Value.ToString();
_form1.comboBox1.Text = row.Cells[1].Value.ToString();

But things get simpler if you use data binding. See: A Detailed Data Binding Tutorial

1.Pass it as cunstrctor parameter:

public Form2(string text){
      Textbox1. text = text;
}

and

Form2 f = new Form2("something to send to the form");
f.Show();

2.Create a public property for Form2:

public string TheText {get{return TextBox1.Text;}; set {textBox1.Text = value;};}

and then from first form:

Form2 f = new Form2();
f.TheText = "Some text";
f.Show();

Either pass the data in the constructor of your other form, if it's mandatory. Or make a public method available in your other form that allows you to set the data separately.

Eg

public void setTextBoxData(String text) { ,etc, etc }

You can then call that method on your second form, passing the value you require from the first form.

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