20

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.

Community
  • 1
  • 1
Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
  • 1
    Are you setting any other properties on your DataGridView? I just tried running this code and the boxes appear checked, and can be cleared using the mouse as normal. Perhaps you have disabled editing somewhere else in your code. – andypaxo Jun 29 '12 at 21:38
  • Seconding @andypaxo, this code works well out of the box in a new WinForms app, so the problem must be somewhere in the context. Please provide some more info/code. – Alan Jun 29 '12 at 21:42
  • can you please post the code in the `designer.cs` file that creates the DataGridView and CheckBox column so we can tell you whats wrong. Ta – Jeremy Thompson Jun 30 '12 at 06:20
  • @JeremyThompson I'll post the code in a few days when I'm back at the office. – Ricardo Altamirano Jun 30 '12 at 13:00

7 Answers7

41

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.

Luis Quijada
  • 2,345
  • 1
  • 26
  • 31
  • 4
    You were correct; I was missing some code from my sample, since part of it still remained in the designer. The `ReadOnly` property was the problem. – Ricardo Altamirano Jul 02 '12 at 14:09
  • 1
    It happened to me time ago.. somehow I changed that property in the designer and resulted in a total waste of time until I discover the key. – Luis Quijada Jul 02 '12 at 15:45
  • 1
    same thing happened to me now. Really helped me too. I changed this property and now faced too much problem and lot waste of time – Abdul Rehman Sep 29 '13 at 23:25
8

Just change the readonly property of DataGridView

     MyDataGridView.ReadOnly = false; 
Abdul Rehman
  • 2,224
  • 25
  • 30
8

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

Heider Sati
  • 2,476
  • 26
  • 28
5

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

Harry
  • 1,765
  • 1
  • 12
  • 12
1

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;
    }
}
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
1

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);
0

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.

gotorg
  • 147
  • 1
  • 10