I have a datagridview showing in a UI with a list of devices. I want to force the user to select the entire row instead of a single cell... Googling for awhile it looks like there is no way to force row selection using the methods supplied for datagridview.
I did this, which works 50% of the time, some times, however it doesn't. Any ideas?
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[dataGridView1.SelectedCells[0].OwningRow.Index].Selected = true;
}
There is an easy way, just set the SelectionMode
to FullRowSelect
:
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
This code works fine to Select the whole row when the user clicks on any cell in the DGV:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
dataGridView1.Rows[e.RowIndex].Selected = true;
}
-Or- the simple ever way is this (from project's "Properties"):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With