user1577242
user1577242

Reputation: 413

How to get checkbox value from a gridview with a child gridview

Can somebody tell me how can I get the list of checked lines (checkbox value from nested grid(ID="nestedGridView") and docnumber value for each selected row) when i click on a button?

<asp:GridView ID="gvMaster" runat="server" AllowPaging="True"
 AutoGenerateColumns="False" DataKeyNames="Accountkey"
 DataSourceID="SqlDataSource1" OnRowDataBound="gvMaster_RowDataBound"> 
   <Columns>  
     <asp:TemplateField> 
       <ItemTemplate>  
         <a href="javascript:cx('customerID-<%# Eval("Accountkey") %>');">
           <img id="imagecustomerID-<%# Eval("Accountkey") %>"
            alt="Click to show/hide orders" border="0" src="Border.png" />
         </a> 
         <asp:CheckBox ID="chkselect" Checked="false" runat="server" />
       </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="Accountkey" /> 
     <asp:BoundField DataField="fullname" /> 
     <asp:TemplateField> 
       <ItemTemplate>  
         <tr><td colspan="100%">  
           <div id="customerID-<%# Eval("Accountkey") %>" style="..."> 
             <asp:GridView ID="nestedGridView" runat="server"
              AutoGenerateColumns="False" DataKeyNames="Id"> 
               <Columns> 
                 <asp:TemplateField> 
                   <ItemTemplate>  
                     <asp:CheckBox ID="chkselect" Checked="false"
                      runat="server" />
                   </ItemTemplate> 
                 </asp:TemplateField>
                 <asp:BoundField DataField="Id" HeaderText="Id"/> 
                 <asp:BoundField DataField="Valuedate" HeaderText="Valuedate"/>
                 <asp:BoundField DataField="Docnumber" HeaderText="Docnumber"/>
               </Columns>
             </asp:GridView>
           </div>  
         </td></tr>  
       </ItemTemplate>  
     </asp:TemplateField> 
   </Columns>  
</asp:GridView>

Upvotes: 0

Views: 5531

Answers (2)

Waqar Janjua
Waqar Janjua

Reputation: 6123

First get a reference to the child GridView, then use FindControl to get the CheckBox inside it:

foreach (GridViewRow row in gvMaster.Rows) 
{
    if (row.RowType == DataControlRowType.DataRow) 
    {
        GridView gvChild = (GridView) row.FindControl("nestedGridView");
        // Then do the same method for check box column 
        if (gvChild != null)
        {
            foreach (GridViewRow row in gvChild .Rows) 
            {
                if (row.RowType == DataControlRowType.DataRow) 
                {
                    CheckBox chk = (CheckBox) row.FindControl("chkselect");
                    if (chk.Checked)
                    {
                        // do your work
                    }
                }
            }
        }
    }
}

Upvotes: 2

Hassan Boutougha
Hassan Boutougha

Reputation: 3919

You can get it through DataRow GridView and casting control as CheckBox:

foreach (GridViewRow row in gvMaster.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chk = row.FindControl("chkselect") as CheckBox;
        if (chk.Checked)
        {
            // do your work
        }
    }
}

Upvotes: 1

Related Questions