mardi 2 février 2016

Can't bind checkbox in GridView

I have two GridViews. One displays the date and the user the other displays a list and next to each item is a checkbox. When the user clicks on the select row on the first grid, the second grid appears that shows all the items. The problem is the checkboxes are blank. It should be reading in the values from the database and ticking the checkboxes. enter image description here

Code for the gridviews:

<div id="divCVRT">
    <fieldset class="groupbox" >                               
        <div style="width:100%;">
            <asp:UpdatePanel ID="udpCVRT" UpdateMode="Conditional" runat="server">
                <ContentTemplate>
                    <asp:GridView  style="width:75%"  
                        ID="gvCVRT" 
                        ShowHeaderWhenEmpty="true"
                        CssClass="tblResults" 
                        runat="server" 
                        OnRowDataBound="gvCVRT_RowDataBound"  
                        OnSelectedIndexChanged="gridviewParent_SelectedIndexChanged"                           
                        DataKeyField="ID" 
                        DataKeyNames="ChecklistID"
                        AutoGenerateColumns="false"
                        allowpaging="false"
                        AlternatingRowStyle-BackColor="#EEEEEE">
                        <HeaderStyle CssClass="tblResultsHeader" />
                        <Columns>
                            <asp:BoundField DataField="ChecklistID" HeaderText="ID" ></asp:BoundField> 
                            <asp:BoundField DataField="ChecklistDate" HeaderText="Checklist Date" dataformatstring="{0:dd/MM/yyyy}"></asp:BoundField>
                            <asp:BoundField DataField="User" HeaderText="User" ></asp:BoundField>
                            <asp:CommandField ShowSelectButton="True" HeaderText="Select" />
                        </Columns>
                    </asp:GridView>
                </ContentTemplate>
            </asp:UpdatePanel>  
            <asp:LinkButton title="Add CVRT" id="btnAddCVRT" OnClick="btnAddCVRT_Click" runat="server" style="cursor:pointer;">
                <img src="../images/icons/buttons/basic1-072.png" />
            </asp:LinkButton>
        </div>
    </fieldset>
</div>
<div id="divCVRTDetails" style="display:none">
    <fieldset class="groupbox" >                               
        <div style="width:100%;">
            <asp:UpdatePanel ID="udpCVRTDetails" UpdateMode="Conditional" runat="server">
                <ContentTemplate>
                    <asp:GridView  style="width:75%"  
                        ID="gvCVRTDetails" 
                        ShowHeaderWhenEmpty="true"
                        CssClass="tblResults" 
                        runat="server" 
                        OnRowDataBound="gvCVRTDetails_RowDataBound"                             
                        DataKeyField="ID" 
                        AutoGenerateColumns="false"
                        allowpaging="false"
                        AlternatingRowStyle-BackColor="#EEEEEE">
                        <HeaderStyle CssClass="tblResultsHeader" />
                        <Columns>
                            <asp:BoundField DataField="ChecklistID" HeaderText="ID" ></asp:BoundField> 
                            <asp:BoundField DataField="Description" HeaderText="Checklist Items"></asp:BoundField> 
                            <asp:TemplateField HeaderText ="Checked?" ItemStyle-HorizontalAlign="Center">
                                <ItemTemplate>
                                         <asp:CheckBox ID="chkChecked"  Checked='<%#bool.Parse(Eval("Checked").ToString())%>' runat="server" ></asp:CheckBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="btnUpdateCVRT" />
                    <asp:AsyncPostBackTrigger ControlID="btnRefreshUpdatePanel" />
                    <asp:AsyncPostBackTrigger ControlID="btnAddCVRT" />
                </Triggers>
            </asp:UpdatePanel> 
        </div>
    </fieldset>

When select is ticked it binds the second grid:

protected void gridviewParent_SelectedIndexChanged(object sender, EventArgs e)
    {
        List<lookupCVRT> workDetails = lookupCVRT.GetChecklistItemsByChecklistID(Company.Current.CompanyID, ParentID.ToString(), gvCVRT.SelectedDataKey.Value.ToString());
        gvCVRTDetails.DataSource = workDetails;
        gvCVRTDetails.DataBind();
        FireJavascriptCallback("setArgAndPostBack ();");

    }

Code behind second grid:

protected void gvCVRTDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            lookupCVRT work = (lookupCVRT)e.Row.DataItem;
            GridView gv = sender as GridView;

            e.Row.Attributes.Add("ID", "gvCVRTDetails_" + work.ID);
            e.Row.Cells[0].Attributes.Add("onclick", "event.stopPropagation();");

            HyperLink ChecklistItem = (HyperLink)e.Row.FindControl("ID");

            CheckBox chkChecked = e.Row.FindControl("chkChecked") as CheckBox;
            if(work.Checked)
            {
                chkChecked.Checked = true;
            }
        }
    }

So I tried binding the checkboxes in the gridview - <asp:CheckBox ID="chkChecked" Checked='<%#bool.Parse(Eval("Checked").ToString())%>' runat="server" ></asp:CheckBox> and also tried chkChecked.Checked = true; in the gvCVRTDetails_RowDataBound function. But nothing is working




Aucun commentaire:

Enregistrer un commentaire