Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force DataGridView Entire Row selection

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;
}
like image 230
bill Avatar asked Jan 10 '23 17:01

bill


2 Answers

There is an easy way, just set the SelectionMode to FullRowSelect:

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
like image 141
etaiso Avatar answered Jan 21 '23 06:01

etaiso


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"):

enter image description here

like image 30
CrownFord Avatar answered Jan 21 '23 06:01

CrownFord