3

I have a datagridview (winforms) with a checkbox column as well as other text-based columns. I've successfully worked through most of the common issues around checkbox columns which are very well documented on this site.

However, I have 1 remaining problem. I am able to click "directly" on a checkbox and it does respond the way I want. However, if I carefully move the mouse pointer between the cell boundary and the checkbox control, and mouse click, I am able to select the cell but the state of the checkbox does not toggle. This problem is much more evident when the row height is bigger for a given row.

Thanks for any help

NOTE: this is not, I repeat NOT, the issue that occurs when focus moves off a given checkbox cell after it is checked. I have that one solved.

1 Answer 1

3

This is not an issue. This is simply how it is supposed to work. For a grid column you can have cellclick events and cellcontentclick events. Since I want the checkbox to check when I click anywhere inside the cell, I should use cellclick. Among other events you will need to listen for, I added the following to my code:

    private void Grid_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if ((e.ColumnIndex == 1) && e.RowIndex != -1)
        {
            this.MyGrid[1, e.RowIndex].Value = !(bool)this.MyGrid[1, e.RowIndex].Value;
            this.MyGrid.EndEdit();
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.