設置DataGridView的屬性SelectionMode爲FullRowSelect
這樣就使DataGridView不是選擇一個字段,而是選擇一整行了
設置DataGridView的屬性MultiSelect爲false
這樣就使DataGridView不能夠選擇多行,只能選擇一行了
想得到某列的值是要判斷DataGridView是否有選中的行
if (dataGridView1.SelectedCells.Count != 0)
{
//得到選中行的索引
int intRow = dataGridView1.SelectedCells[0].RowIndex;
//得到列的索引
int intColumn = dataGridView1.SelectedCells[0].ColumnIndex;
//得到選中行某列的值
string str = dataGridView1.CurrentRow.Cells[2].Value.ToString();
MessageBox.Show(str);
}
//不顯示出dataGridView1的最後一行空白
dataGridView1.AllowUserToAddRows = false;