winforms - value - DataGridView column of type DataGridViewCheckBoxCell is constantly readonly/disabled




how to add checkbox in datagridview in c# dynamically (4)

Create a TemplateField and bound the id to it, something like this:

<asp:TemplateField HeaderText="Whatever" SortExpression="fieldname" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:CheckBox runat="server" ID="rowCheck" key='<%# Eval("id") %>' />
    </ItemTemplate>
</asp:TemplateField>

I am using a .NET Windows Forms DataGridView and I need to edit a DataBound column (that binds on a boolean DataTable column). For this I specify the cell template like this:

DataGridViewColumn column = new DataGridViewColumn(new DataGridViewCheckBoxCell());

You see that I need a CheckBox cell template.

The problem I face is that this column is constantly readonly/disabled, as if it would be of TextBox type. It doesn't show a checkbox at all.

Any thoughts on how to work with editable checkbox columns for DataGridView?

Update: For windows forms, please.

Thanks.


Indeed, you will have to draw the control yourself. On the plus side... drawing the control yourself isn't actually that hard. This is a decent example of drawing your own checkbox (we're using something rather similar in our own code).

Just, instead of overriding it to look disabled, you want to override it to make the box smaller... I don't see any way of calling CheckBoxRenderer.DrawCheckBox with a size, but there shouldn't be anything stopping you from drawing into your own graphics object, shrinking it yourself, then drawing the image you just shrunk.


Well, after more than 4 hours of debugging, I have found that the DataGridView row height was too small for the checkbox to be painted, so it was not displayed at all. I have found this after an accidental row height resizing.

As a solution, you can set the AutoSizeRowsMode to AllCells.

richDataGrid.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;


Is there a way to make DataGridViewCheckBox Cells smaller?

If you are now using .NET 4.0, you can use the DataGridView.RowTemplate to adjust the minimum height.

For example,

DataGridViewRow row = this.dataGridView1.RowTemplate;
row.DefaultCellStyle.BackColor = Color.Bisque;
row.Height = 35;
row.MinimumHeight = 20;

However, as evidenced by this MSDN answer the minimum height for a row with check boxes is 17 pixels. It does not appear there is any way around this problem.