C#——DataGridView选中行,在TextBox中显示选中行的内容

C#——DataGridView选中行,在TextBox中显示选中行的内容,在DataGridView的SelectionChanged实践中设置如下代码

private void dataGridView1_SelectionChanged(object sender, EventArgs e)

{

int index = dataGridView1.SelectedRows[0].Index; //获取选中行的行号

textBox1.Text = dataGridView1.Rows[index].Cells[0].Value.ToString();

textBox2.Text = dataGridView1.Rows[index].Cells[1].Value.ToString();

 

this.textBox_ID.Text = this.dataGridView1.SelectedRows[0].Index.ToString();

this.textBox_CityName.Text=this.dataGridView1.CurrentRow.Cells[1].Value.ToString();//按照这种写法也可以

this.textBox_CityCode.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();

this.textBox_District.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();

this.textBox_Population.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();

}

原文地址:https://www.cnblogs.com/AmatVictorialCuram/p/5066261.html