2

I have a gridview pulling back data from a table in my SQL database. I have a checkbox field in my database set to BIT data value. I need to be able to select a row from the table in asp.net (only one at a time) in order to accept or decline a purchase order.

So far i have the following code

<asp:CheckBoxField DataField="CheckBox" HeaderText="Select" Visible="true" ReadOnly="false" />

which is just pulling back the fact that the column "checkbox" has a checkbox value and not allowing me to select it. All of the demonstrations i have seen of this have been using the datagrid view and inserting a checkbox into there, however, i cant seem to add the fields from my database to this.

Any help welcome

Brandon Boone
  • 16,281
  • 4
  • 73
  • 100

2 Answers2

0

The gridview defaults to ItemMode where it does not allow editing. You'll have to add an edit command and switch to EditMode and then your check box should be editable.

 <Columns>
       ...
       <asp:CommandField ShowEditButton="True" />
       ...
 </Columns>

Alternatively, you can make it a TemplateField and make it editable even in ItemMode.

Bala R
  • 107,317
  • 23
  • 199
  • 210
0

This is by design as, despite being a checkbox field, the rows in a GridView are uneditable by default. The way I've always handled this is with a TemplateField:

<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkSelect" Checked='<%# Eval("Select") %>' />
</ItemTemplate>
</asp:TemplateField>

Then, if you want to only select one row at a time, use something like this (in jQuery 1.6):

var checkboxes = $('input[type="checkbox"][name$="chkSelect"]');
checkboxes.click(function() {
    checkboxes.not(this).prop("checked", false);
});

Working Demo

Town
  • 14,706
  • 3
  • 48
  • 72