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