5

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.

3 Answers 3

6

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;

0
1

Instead of trying to create the column in code, click on the tiny arrow in a box at the top right of the DataGridView control, and select "Edit Columns..." from the menu that appears. In the dialog box, click the Add button, then choose the "Databound column" option and pick the boolean column you're binding to.

1
  • Yes, that would have been a solution, maybe, but in my case the columns were added programatically. Sep 17, 2008 at 8:55
0

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>
1
  • I forgot to specify that I need the answer for Windows Forms. Thanks. Sep 16, 2008 at 11:11

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.