从 TextBox 到 DataGridView 的动态值

Dynamic value from TextBox to DataGridView

所以我创建了一个动态 TextBox 允许用户在需要时输入描述,但是我想用用户的描述填充我的 table。例如

dataGridView1.Rows[n].Cells[3].Value = textDescription.Text

但是每次我这样做的时候,testDescription名字都不可用,我想知道有没有什么办法可以解决这个问题。

只想说我的动态意思是我自己创建了 TextBox 而不是拖放。

private void button1_Click(object sender, EventArgs e)
{
    TextBox textDescription = new TextBox();
    this.Controls.Add(textDescription);
    textDescription.Location = new Point(180, 190);
    textDescription.Size = new Size(154, 20);
    textDescription.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
    textDescription.Text = "";
}

我注意到 TextBox 是 "button1_Click" 中的局部变量。也许您可以尝试将其定义为全局变量。

TextBox textDescription = new TextBox();

private void button1_Click(object sender, EventArgs e)
{
    this.Controls.Add(textDescription);
    textDescription.Location = new Point(180, 190);
    textDescription.Size = new Size(154, 20);
    textDescription.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
    textDescription.Text = "123";
}

private void button2_Click(object sender, EventArgs e)
{
    dataGridView1.Rows[0].Cells[0].Value = textDescription.Text;
}