C# 如何将datagridview的数据以其他形式传递给文本框?

C# 如何将datagridview的数据以其他形式传递给文本框?,c#,visual-studio-2015,C#,Visual Studio 2015,Datagridview位于Form2中,文本框位于Form1中 使用Show()从Form1调用Form2;其中位于dataGridView,然后将此信息传递给Form1中的文本框 格式2中的代码示例: private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { Form1 exportar = new Form1(); exportar.textBox1.T

Datagridview位于Form2中,文本框位于Form1中

使用Show()从Form1调用Form2;其中位于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加倍。

1。将其作为cunstrctor参数传递:

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

2.为Form2创建公共属性:

public string TheText {get{return TextBox1.Text;}; set {textBox1.Text = value;};}
然后从第一种形式:

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

如果强制的话,可以在另一个表单的构造函数中传递数据。或者以其他形式提供一个公共方法,允许您单独设置数据

例如


然后,您可以在第二个表单上调用该方法,从第一个表单中传递所需的值。

您需要在Form2中引用Form1。您可以在Form2的构造函数中传递它

private Form1 _form1;

public Form2 (Form1 form1)
{
    _form1 = form1;
}
在Form1中创建并打开Form2,如下所示:

var form2 = new Form2(this);
form2.ShowDialog(this);
为了能够访问其他窗体的控件,必须在属性窗口中将其
modifier
更改为
Internal

然后您可以设置如下值:

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


但如果使用数据绑定,事情会变得更简单。请参阅:

是否使用类似Form2 f2=new Form2()的内容从Form1调用Form2;f2.Show()?是的,我喜欢。使用Show()调用表单2;dataGridView位于何处,然后将此信息传递给Form1。那么下面由Olivier Jacot Descombes先生给出的答案是正确的。将Form1的实例传递给被调用的Form2实例。这允许Form2中的代码正确引用文本框可见的表单。您不应该创建Form1的另一个实例
var form2 = new Form2(this);
form2.ShowDialog(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();