c# winform DataGridView單擊選中一整行,只能單選,不能選擇多行,只能選擇一行
設置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);
}