Bserk
Bserk

Reputation: 91

C# anyone see why this doesn't correctly select row in a datagridview

i have a datagridview and i would like the rowheader to correctly select that entire row. Although i thought it should anyway, it does not. i have tried the following but with no luck, can you see something obvious? =P regards, Dave

    private void dataGridView2_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        dataGridView2.Rows[e.RowIndex].Selected = true;
    }

Upvotes: 2

Views: 1781

Answers (1)

reggie
reggie

Reputation: 13721

Try setting the

DataGridView.MultiSelect=false; 

and

DataGridView.SelectionMode = FullRowSelect; 

You can read about the MultiSelect Property and SelectionMode Property in the MSDN library linked.

If you want the user to select multiple rows, then set MultiSelect to true.

 DataGridView.MultiSelect=true; 

EDIT

And then you can call your event like this:

private void dataGridView2_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    dataGridView2.Rows[e.RowIndex].Selected = true;
}

To select individual cells within the data grid view and select the entire row on row header click, set the selection mode to RowHeaderSelect

DataGridView.SelectionMode = RowHeaderSelect;

The MSDN explanation for RowHeaderSelect is: Clicking a cell selects it. Clicking a row header selects the entire row.

Upvotes: 4

Related Questions