C#——winfrom——DataGridView 选中某一行的事件


先将SelectionMode属性设置一下,改为fullrowselection. 然后给一个cellclick事件 注意:点击表头时也会触发此事件,在取值时要排除

private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
  //当点击表头部的列时,e.RowIndex==-1
  if (e.RowIndex > -1)
  {
    this.txtUsername.Text = this.dataGridView2.Rows[e.RowIndex].Cells[1].Value.ToString();
    this.txtPassword.Text = this.dataGridView2.Rows[e.RowIndex].Cells[2].Value.ToString();
    this.txtPosition.Text = this.dataGridView2.Rows[e.RowIndex].Cells[3].Value.ToString();
    this.txtStatus.Text = this.dataGridView2.Rows[e.RowIndex].Cells[6].Value.ToString();
    this.txtName.Text = this.dataGridView2.Rows[e.RowIndex].Cells[7].Value.ToString();
  }

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM