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