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