'Cannot check box in DataGridViewCheckBoxColumn?

I'm creating a simple DataGridView with a check box column and a text column (more columns will follow, but this is the minimal working example that I'm trying to get working). When I run this code, the checkbox columns appears, but I can't check the boxes.

DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.ThreeState = false;
checkColumn.Width = 20;

MyDataGridView.Columns.Add(checkColumn);
MyDataGridView.Columns.Add(new DataGridViewTextBoxColumn());

Since nothing appears in this case, I thought to add some dummy data.

for (int i = 0; i < 10; i++)
{
    MyDataGridView.Rows.Add(new Object[] { true, "test"});
}

Normally, the DataGridView is populated with data bound from a list of custom objects, like in this question of mine, but I thought it would be better to get this working in a basic way before moving on.

I'm not trying to set the checked state programmatically, but rather let the user select and then use that selection in various other event handlers.



Solution 1:[1]

The code seems to be fine, so I just can tell you to check and ensure that the following DataGridView properties are properly set: ReadOnly set to False and Enabled set to True.

Solution 2:[2]

I had the same problem, the solution for me was to change the

"EditMode" from "EditProgramatically" into the default of "EditOnKeystrokeOrF2",

this solved my issue.

All the above suggestions were already implemented.

Kind Regards Heider

Solution 3:[3]

Just change the readonly property of DataGridView

     MyDataGridView.ReadOnly = false; 

Solution 4:[4]

The table itself may be set to Read-Only even though the checkbox column isn't the table setting will override the column setting.

Solution 5:[5]

This can happen as well when you populate the DataGridView with an object, that has public Boolean property (the CheckBox in the DataGridView) that has a private setter. A column in the DataGridView that represents this (read-only) property is automatically read-only as its not allowed to set the property externally (=out side the code of the object).

public class ExampleObject
{
    /// <summary>
    /// A public property that can only be read.
    /// When a DataGridViewRow is populated with this object, the column representing this Boolean property is automatically read-only.
    /// </summary>
    public Boolean QCPassed
    {
        get;
        private set;
    }
}

Solution 6:[6]

I had also the same issue with different situations My DataGridView was bound to a DataTable which was filled by a SqlDataReader (which is read-only). I replaced SqlDataReader with SqlDataAdapter works fine.

DataTable dt=new DataTable();
SqlDataAdapter da=new SqlDataAdapter("Select <column_names> from <table_name>",con);
da.Fill(dt);

Solution 7:[7]

I found that a DataGridViewCheckBoxCell in a selected row won't respond to mouse clicks on the checkbox if a) the DataGridView is set to SelectionMode=FullRowSelect and b) the method handling the DataGridView.CellMouseMove event was firing off a DataGridView.DoDragDrop function. To get around this, I had to not call the DoDragDrop if the cell triggering the CellMouseMove event was a DataGridViewCheckBoxCell.

Yet again, the DataGridView shows itself to be extremely finicky to the would-be power user.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luis Quijada
Solution 2 Heider Sati
Solution 3 Abdul Rehman
Solution 4 Harry
Solution 5 Mike de Klerk
Solution 6
Solution 7 gotorg