C#如何在textBox1中显示DataGridView中选定的行?

C#如何在textBox1中显示DataGridView中选定的行?,c#,database,datagridview,C#,Database,Datagridview,您好,我正在尝试将所选行从DataGridView1显示到textBox1。 DataGridView中显示的数据来自数据库“LocalDB”。 我只是想让“CustomerName”显示在textBox1中。 因为我需要文本框1中显示的“CustomerName”,以便从数据库中删除它。 我还不确定,但这是我的代码: 您是否尝试了SelectionChanged事件?我将使用SelectionChanged事件?是否引发异常?因为如果代码正常,唯一不好的就是列“CustomerName”的名称

您好,我正在尝试将所选行从DataGridView1显示到textBox1。 DataGridView中显示的数据来自数据库“LocalDB”。 我只是想让“CustomerName”显示在textBox1中。 因为我需要文本框1中显示的“CustomerName”,以便从数据库中删除它。 我还不确定,但这是我的代码:


您是否尝试了SelectionChanged事件?我将使用SelectionChanged事件?是否引发异常?因为如果代码正常,唯一不好的就是列“CustomerName”的名称-检查列是否有相同的名称你好,谢谢!我将代码更改为:
int index=e.RowIndex;//获取行索引DataGridViewRow selectedRow=dataGridView1.Rows[Index];textBox1.Text=selectedRow.Cells[0].Value.ToString()
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
                textBox1.Text = row.Cells["CustomersName"].Value.ToString();
            }
        }