Datagridview位于Form1中的Form2,TextBoxes中。如何将datagridview的数据传递给其他表单中的文本框?
用Show()从Form1中调用Form 2;其中位于dataGridView,然后将此信息传递给Form1中的文本框。在窗体2
代码示例:
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();
}
这没有工作,但是当我把exportar.Show()
传递的信息。问题是,Form1增加了一倍。
您是否使用类似_Form2的方式从Form1调用Form2 f2 = new Form2(); f2.Show(); _? – Steve
是的,我喜欢。使用Show()调用Form 2;在哪里找到dataGridView,然后将这些信息传递给Form1。 – Ale
那么从Olivier Jacot-Descombes先生那里得到的答案是正确的。您将Form 1的实例传递给被调用的Form 2实例。这允许Form2内的代码正确引用TextBoxes可见的窗体。你不应该创建Form1的另一个实例 – Steve