views:

2672

answers:

3

Hello,

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.

A: 

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>
Biri
I forgot to specify that I need the answer for Windows Forms. Thanks.
Vasi
Sorry, I've missed that point.
Biri
A: 

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.

Phillip Wells
Yes, that would have been a solution, maybe, but in my case the columns were added programatically.
Vasi
+5  A: 

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;

Vasi