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