Sreenath Ganga
Sreenath Ganga

Reputation: 736

select a row in datagridview programmatically

I had a data grid with more than 100 rows on it if the user click the first column of the row the whole row must be selected and if he click on any other column it must not select the whole row I set the selection mode of datagridview cellselection and made it read only also and tried all the below code but found no use can any one suggest any idea

private void tbljobdata_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (tbljobdata.CurrentCell.ColumnIndex == 0)
    {
        tbljobdata.Rows[tbljobdata.CurrentCell.RowIndex].Selected = true;
    }
}

Upvotes: 1

Views: 4840

Answers (2)

Amit
Amit

Reputation: 22076

Please try this code:

private void tbljobdata_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        tbljobdata.Rows[e.RowIndex].Selected = true;
    }
}

For further reading on CellClick event here is Docs link.

Upvotes: 1

nawfal
nawfal

Reputation: 73183

Handle mouse down event since thats captured the first.

private void tbljobdata_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.ColumnIndex == 0)
        tbljobdata.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    else
        tbljobdata.SelectionMode = DataGridViewSelectionMode.CellSelect;
}

Alter the read only property too accordingly under the condition required.. you can set readonly for individual cells in fact if you want

Upvotes: 2

Related Questions