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

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM