2

I am using Nested GridViews where each row in the gridview has child gridView. I am using RowDataBound Event of Parent GridView, to Binding Child GridView. My Problem is that, how to get Child GridView's Button findcontrol value in Parent gridViews RowDataBound Event.

This is my Aspx page

         <asp:GridView ID="grdSubClaimOuter" SkinID="GridView" runat="server" Width="100%"
                    AutoGenerateColumns="false" OnRowDataBound="grdSubClaimOuter_RowDataBound" OnRowCommand="grdSubClaimOuter_RowCommand"
                    ShowFooter="false" AllowPaging="true" OnPageIndexChanging="grdSubClaimOuter_PageIndexChanging">
                    <%--<AlternatingRowStyle BackColor="ButtonFace" />--%>
                    <Columns>
                        <asp:TemplateField ItemStyle-Width="5%">
                            <ItemTemplate>
                                <asp:HiddenField ID="hdnClaimNo" runat="server" Value='<%# Eval("ClaimNo") %>' />
                                <asp:Image runat="server" ID="img1" ImageUrl="../images/Collapse_plus.png" />
                            </ItemTemplate>
                            <ItemStyle Width="5%"></ItemStyle>
                        </asp:TemplateField>
              <asp:GridView ID="grdSubClaim" runat="server" SkinID="GridView" CellPadding="4" Width="100%"
                                    AutoGenerateColumns="false" ShowFooter="false" OnRowEditing="grdSubClaim_RowEditing"
                                    OnRowCommand="grdSubClaim_RowCommand" OnRowDeleting="grdSubClaim_RowDeleted" 
                                    AllowPaging="false" >
                                    <%--SkinID="GridView"--%>
                                    <Columns>
                                        <asp:TemplateField FooterStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
                                            <HeaderTemplate>
                                                Sub Claim No
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="lblSubClaimNoValue" Width="" runat="server" Text='<%#Eval("SubClaimNo")%>'></asp:Label>
                                            </ItemTemplate>
                                            <FooterStyle HorizontalAlign="Left" />
                                        </asp:TemplateField>
           <asp:Button ID="btnSubrogation" CssClass="groovybutton" runat="server" CommandName="Subrogation"
                                                    Text="Subrogation" CommandArgument='<%# Eval("ClaimNo") + "~" + Eval("SubClaimNo")%>' />
                                                <asp:Button ID="btnSalvage" CssClass="groovybutton" runat="server" CommandName="Salvage"
                                                    Text="Salvage" CommandArgument='<%# Eval("ClaimNo") + "~" + Eval("SubClaimNo")%>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                    <AlternatingRowStyle CssClass="" />
                                    <RowStyle CssClass="ob_gBody" />
                                    <HeaderStyle CssClass="gridHeader" />
                                </asp:GridView>
                                <asp:Literal runat="server" ID="Literal2" Text="</td></tr>" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

This is My aspx.cs file

 protected void grdSubClaimOuter_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[1].Text.ToString() != "&nbsp;")
        {
            Literal ltrChild = (Literal)e.Row.FindControl("ltrChild");
            System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)e.Row.Cells[0].FindControl("img1");

            ltrChild.Text = ltrChild.Text.Replace("trChildGrid", "trChildGrid" + e.Row.RowIndex.ToString());
            string strChildGrid = "trChildGrid" + e.Row.RowIndex.ToString();
            e.Row.Cells[0].Attributes.Add("OnClick", "OpenTable('" + strChildGrid + "','" + img.ClientID + "')");
            e.Row.Cells[0].RowSpan = 1;
            System.Web.UI.WebControls.GridView gvChild = (System.Web.UI.WebControls.GridView)e.Row.FindControl("grdSubClaim");
            PolicyProcessor.DAL.Claim.ClaimSubClaim objDALClaimSubClaim = new PolicyProcessor.DAL.Claim.ClaimSubClaim();
            PolicyProcessor.BOL.Claim.ClaimSubClaim objInfoClaimSubClaim = new PolicyProcessor.BOL.Claim.ClaimSubClaim();
            HiddenField hdnClaimNo = (HiddenField)e.Row.FindControl("hdnClaimNo");

            if (hdnClaimNo.Value != "")
            {
                objInfoClaimSubClaim.ClaimNo = hdnClaimNo.Value;
            }
            else
            {
                objInfoClaimSubClaim.ClaimNo = "0";
            }

            DataSet dsChild;
            dsChild = objDALClaimSubClaim.ResultSet(objInfoClaimSubClaim, "SelectInnerGrid");
            if (dsChild.Tables[0].Rows.Count > 0)
            {


                Button btn = (Button)gvChild.FindControl("btnSalvage");

           //btn is null how to get text value in btn

                btn.ForeColor = System.Drawing.Color.Red;


                gvChild.DataSource = dsChild;
                gvChild.DataBind();

            }
            else
            {
                Helper.EmptyGrid(gvChild, dsChild.Tables[0]);
            }

        }

    }
}

if anyone knows it,please help me solve this problem.thanks in advance.

1
  • The question is unclear ? as the parent grid would have n number of child based button which are inside the grid. Can you explain a bit further. Commented Aug 25, 2012 at 7:12

2 Answers 2

6

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

foreach (GridViewRow row in grdSubClaimOuter.Rows) 
{
if (row.RowType == DataControlRowType.DataRow) 
{
    GridView gvChild = (GridView) row.FindControl("grdSubClaim");
    // Then do the same method for Button control column 
    if (gvChild != null)
    {
        foreach (GridViewRow row in gvChild .Rows) 
        {
            if (row.RowType == DataControlRowType.DataRow) 
            {
                Button btn = (Button ) row.FindControl("buttonID");
                if (btn != null )
                {
                    // do your work
                }
            }
        }
    }
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank u very much @Waqar Janjua .I used your answer i get the result Thanks
0

If you want value in parent grid's data bound event then use either way

1)You can also use your dsChild(Your dataset) and get field value which you are binding for button.

2) Get value of button from gvChild after binding child grid.

No need to loop into parent and child grid.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.