如何将数据视图选定行的值复制到文本框中

本文关键字:复制 文本 数据 视图 | 更新日期: 2023-09-27 18:05:29

我正在尝试选择一个datagridview行并复制值到3个文本框。

我尝试了这个代码(它在我的另一个项目中工作),但我得到了一个错误。这是代码:

private void DataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            DataGridViewRow row = this.DataGridView2.Rows[e.RowIndex];
            textBox1.Text = row.Cells["HomeNM"].Value.ToString();
            textBox3.Text = row.Cells["HostNM"].Value.ToString();
            fromTxt.Text = row.Cells["odd1NM"].Value.ToString();
        }
    }

这是我的错误:

Error 1 ' bexscraping.Form1 ' does not contain a definition of ' DataGridView2 ' and has not been found no extension method ' DataGridView2 ' accepting a first argument of type ' bexscraping.Form1 ' . Probably missing a using directive or a reference to an assembly.

我不知道错误在哪里-你知道吗?

EDIT:我忘了初始化我的datagridview;现在我的代码正在工作!无论如何,谢谢!

如何将数据视图选定行的值复制到文本框中

确保gridView已初始化。然后试试

private void dataGridView1_Click(object sender, EventArgs e)
    {
        if(dataGridView1.SelectedRows.Count>0){
           textBox1.Text = dataGridView1.SelectedRows[0].Cells["HomeNM"].Value.ToString();
           textBox2.Text = dataGridView1.SelectedRows[0].Cells["HostNM"].Value.ToString();
            textBox3.Text = dataGridView1.SelectedRows[0].Cells["odd1NM"].Value.ToString();
    }
   } // haha

首先确保名称DataGridView2是正确的。

然后打开设计器,单击DataGridView2,并在属性中确保Design -> GenerateMember设置为true。

试试这个,先生。

if (e.RowIndex >= 0)
{
 txtbox1.text = datagridview1.currentrow.cells["cellname"].value
 . . . 'and so on.
}

希望这将解决您的错误