0

VS2010 .net

I have a gridview that displays information from a database. One of the fields is a checkbox. What I would like to do is be able to click the checkbox and have it update back to the database, but the checkbox field is grayed-out. Below is my markup. searching for making checkboxes clickable does not return any helpful results.

<asp:GridView ID="gvSiteInfo" runat="server" BackColor="White" 
    GridLines="Vertical" AutoGenerateColumns="False" OnSorting="gvSiteInfo_Sorting" 
    onselectedindexchanged="gvSiteInfo_SelectedIndexChanged" 
    AllowSorting="True">
    <Columns>
        <asp:BoundField DataField="prodServer" HeaderText="Production Server" 
            SortExpression="prodServer" />
        <asp:BoundField DataField="prodHostHeader" HeaderText="Production Host Header" 
            SortExpression="prodHostHeader" />
        <asp:BoundField DataField="prodIP" HeaderText="Production IP Address" 
            SortExpression="prodIP" />
        <asp:CheckBoxField DataField="testComplete" HeaderText="Testing Completed" 
            SortExpression="testComplete" />
    </Columns>
</asp:GridView>
Mike
  • 437
  • 3
  • 8
  • 18

1 Answers1

1

You should use a templatefield:

                       <asp:TemplateField>
                            <ItemTemplate>
                                <asp:CheckBox ID="chkSelector" runat="server"
                                    ToolTip="Select row?" />
                            </ItemTemplate>
                        </asp:TemplateField>

You also do not seem to set the value based on the database:

Checked='<%# DataBinder.Eval(Container, "DataItem.IsChecked") %>'
JonH
  • 32,732
  • 12
  • 87
  • 145
  • When i use `` I get an "InvalidCastException was unhandled by user code" error. However, the database is returning either 'true' or 'false' so i don't understand why there is an error. – Mike Aug 07 '12 at 15:40
  • problem solved. the 'checked' was looking for a boolean, and i thought passing it 'true' or 'false' would satisfy this. but what i had to do was `Checked='<%# DataBinder.Eval(Container, "DataItem.testComplete").toString().Equals("true")` – Mike Aug 07 '12 at 16:19
  • @Mike great job, yes if you are storing them in the db as "true" or "false". – JonH Aug 07 '12 at 16:19