Click here to Skip to main content
15,905,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a parent grid which calls two child grids. But for some unknown reason they are getting fired twice. I cant't really seem to figure out the problem with this code. Can anyone guide me?

Backend Code (C#):

C#
protected void grdGrpPrntRpt_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       try
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               TextBox litGrp = (TextBox)e.Row.FindControl("litGrp");
               litGrp.Text = DataBinder.Eval(e.Row.DataItem, "GroupName").ToString();
               ViewState["GroupName"] = litGrp.Text;
               DataSet ds = (DataSet)ViewState["ds"];
               DataView dv = ds.Tables[3].DefaultView;
               dv.RowFilter = "GroupName='" + litGrp.Text + "'";
               DataTable dt = dv.ToTable();
               GridView gvinner = (GridView)e.Row.FindControl("grdInnerMain");
               gvinner.DataSource = dt;
               gvinner.DataBind();
           }
       }
       catch (Exception Ex)
       {
           //report error
       }
   }


   protected void grdInnerMain_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       try
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               TextBox litUsr = (TextBox)e.Row.FindControl("litUsr");
               litUsr.Text = DataBinder.Eval(e.Row.DataItem, "FirstName").ToString();
               // string username=
               DataSet ds = (DataSet)ViewState["ds"];
               DataView dv = ds.Tables[3].DefaultView;
               dv.RowFilter = "userName='" + DataBinder.Eval(e.Row.DataItem, "UserName").ToString().Replace("'", "''") + "' and GroupName='" + ViewState["GroupName"].ToString() + "'";
               DataTable dt = dv.ToTable();
               totalPages = 0;
               totalCost = 0;
               GridView gvinner2 = (GridView)e.Row.FindControl("innerGrid");
               gvinner2.DataSource = dt;
               gvinner2.DataBind();
           }
       }

       catch (Exception Ex)
       {
           //report error
       }

   }
   protected void innerGrid_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       try
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               DataView dv = ((DataSet)ViewState["dspaper"]).Tables[0].DefaultView;
               dv.RowFilter = "paperid=" + Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "paperid").ToString());
               DataTable dt = dv.ToTable();
               e.Row.Cells[0].Text = dt.Rows[0]["PaperName"].ToString();
               //  e.Row.Cells[2].Text = DataBinder.Eval(e.Row.DataItem, "TotalPagesPrinted").ToString() == "0" ? DataBinder.Eval(e.Row.DataItem, "TotalPagesSent").ToString() : DataBinder.Eval(e.Row.DataItem, "TotalPagesPrinted").ToString();
               e.Row.Cells[2].Text = DataBinder.Eval(e.Row.DataItem, "TotalPage").ToString() == "0" ? DataBinder.Eval(e.Row.DataItem, "Pages_Sent").ToString() : DataBinder.Eval(e.Row.DataItem, "TotalPage").ToString();
               // e.Row.Cells[3].Text = Convert.ToDouble(e.Row.Cells[3].Text.ToString()).ToString("0.00");
               totalPages += Convert.ToInt32(e.Row.Cells[2].Text.ToString());
               totalCost += Convert.ToDouble(e.Row.Cells[3].Text.ToString());
               e.Row.Cells[1].Text = DataBinder.Eval(e.Row.DataItem, "Color").ToString() == "0" ? "B & W" : "Color";
           }
           if (e.Row.RowType == DataControlRowType.Footer)
           {
               e.Row.Cells[2].Text = totalPages.ToString();
               e.Row.Cells[3].Text = totalCost.ToString("0.00");
           }
           if (e.Row.RowType == DataControlRowType.Header)
           {
               e.Row.Cells[0].Text = CResourcesManager.GetString("grdHeaderMediaReport");
               e.Row.Cells[1].Text = CResourcesManager.GetString("grdHeaderColorReport");
               e.Row.Cells[2].Text = CResourcesManager.GetString("grdHeaderPagesReport"); ;
               e.Row.Cells[3].Text = CResourcesManager.GetString("grdHeaderTotalCostReport");
           }
       }
       catch
       { }
   }


Frontend Code:

ASP.NET
<asp:GridView Width="100%" ID="grdGrpPrntRpt" runat="server" AutoGenerateColumns="False"
                        CellPadding="0" CellSpacing="2" BorderWidth="0px" ShowHeader="false" RowStyle-HorizontalAlign="Left"
                        OnRowDataBound="grdGrpPrntRpt_RowDataBound">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <table id="tblInnerHeader"  runat="server" width="100%" cellspacing="0" cellpadding="0">
                                        <tr bgcolor="gray" valign="top">
                                            <td width="20%" height="30px">
                                                 
                                                <asp:TextBox ID="litGrp" ForeColor="White" BackColor="Gray" CssClass="LabelText2"
                                                    runat="server" BorderStyle="None" BorderWidth="0"></asp:TextBox>
                                            </td>
                                            <td width="2%">
                                            </td>
                                            <td width="78%" align="left">
                                                <asp:ImageButton ID="imgMainShowHide" runat="server" ImageUrl="~/Images/plus.jpg"
                                                    OnClick="imgMainShowHide_Click" />
                                            </td>
                                        </tr>
                                    </table>
                                    <table id="tblFirstInnerGrid"  runat="server" visible="false" width="100%" cellspacing="2"
                                        cellpadding="0">
                                        <tr>
                                            <td align="center">
                                                <asp:GridView Width="100%" ID="grdInnerMain" runat="server" AutoGenerateColumns="False"
                                                    CellPadding="0" CellSpacing="2" BorderWidth="0px" ShowHeader="false" RowStyle-HorizontalAlign="Left"
                                                    OnRowDataBound="grdInnerMain_RowDataBound">
                                                    <Columns>
                                                        <asp:TemplateField>
                                                            <ItemTemplate>
                                                                <table id="tblHeader"  runat="server" width="100%" cellspacing="0" cellpadding="0">
                                                                    <tr bgcolor="Wheat" valign="top">
                                                                        <td width="20%" height="25px">
                                                                             
                                                                            <asp:TextBox ID="litUsr" ForeColor="Blue" BackColor="Wheat" CssClass="LabelText2"
                                                                                runat="server" BorderStyle="None" BorderWidth="0"></asp:TextBox>
                                                                        </td>
                                                                        <td width="2%">
                                                                        </td>
                                                                        <td width="78%" align="left">
                                                                            <asp:ImageButton ID="imgShowHide" runat="server" ImageUrl="~/Images/plus.jpg" OnClick="imgShowHide_Click" />
                                                                        </td>
                                                                    </tr>
                                                                </table>
                                                                <table id="tblInnerGrid"  runat="server" visible="false" width="100%" cellspacing="2"
                                                                    cellpadding="0">
                                                                    <tr>
                                                                        <td align="center">
                                                                            <asp:GridView ID="innerGrid" runat="server" AutoGenerateColumns="False" CellPadding="0"
                                                                                CellSpacing="2" HeaderStyle-CssClass="GridHeaderNoSorting" BorderWidth="0px"
                                                                                ShowFooter="true" RowStyle-HorizontalAlign="Left" Width="98%" OnRowDataBound="innerGrid_RowDataBound">
                                                                                <Columns>
                                                                                    <asp:TemplateField>
                                                                                        <ItemTemplate>
                                                                                            <%-- <asp:Label ID="lblMedia" runat="server"></asp:Label>--%>
                                                                                        </ItemTemplate>
                                                                                    </asp:TemplateField>
                                                                                    <%--<asp:BoundField DataField="Color" HeaderText="Color/ B & W" /> --%>
                                                                                    <asp:TemplateField HeaderText="Color/ B & W">
                                                                                        <ItemTemplate>
                                                                                            <%--   <asp:Label ID="lblColor" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Color").ToString()=="0" ? "B & W" :"Color" %>'></asp:Label>--%>
                                                                                        </ItemTemplate>
                                                                                    </asp:TemplateField>
                                                                                    <%--<asp:BoundField DataField="TotalPagesPrinted" ItemStyle-HorizontalAlign="Center" />--%>
                                                                                    <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"
                                                                                        FooterStyle-HorizontalAlign="Center">
                                                                                        <ItemTemplate>
                                                                                            <%--<asp:Label ID="lblPagesPrinted" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Color").ToString()=="0" ? "B & W" :"Color" %>'></asp:Label>--%>
                                                                                        </ItemTemplate>
                                                                                    </asp:TemplateField>
                                                                                    <asp:BoundField DataField="Total_Cost" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"
                                                                                        FooterStyle-HorizontalAlign="Center" />
                                                                                </Columns>
                                                                                <HeaderStyle CssClass="GridHeaderNoSorting" />
                                                                                <AlternatingRowStyle CssClass="GridAlternateRowStyle" />
                                                                                <RowStyle CssClass="GridRowStyle" />
                                                                                <FooterStyle CssClass="GridFooter" HorizontalAlign="Left" />
                                                                            </asp:GridView>
                                                                        </td>
                                                                    </tr>
                                                                </table>
                                                            </ItemTemplate>
                                                        </asp:TemplateField>
                                                    </Columns>
                                                    <AlternatingRowStyle CssClass="GridAlternateRowStyle" />
                                                    <RowStyle CssClass="GridRowStyle" />
                                                </asp:GridView>
                                            </td>
                                        </tr>
                                    </table>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <AlternatingRowStyle CssClass="GridAlternateRowStyle" />
                        <RowStyle CssClass="GridRowStyle" />
                    </asp:GridView>
Posted
Comments
Thanks7872 16-Oct-13 2:51am    
Remove unnecessary code block as front end code is not at all required. What do you mean by calls two child grids? GridView is not something to be called. You also not specified what fired twice?
BlehBlahBah 16-Oct-13 2:54am    
the entire gridview is getting fired twice. there are two inner grids inside the main gridview.
Thanks7872 16-Oct-13 2:55am    
GridView is not something that gets fired. What do you mean by that? Explain me what you wanted to do and what is happening.
BlehBlahBah 16-Oct-13 5:19am    
I basically want to bind the grid on event click. There are three grids which are bound. The first one which displays the header, then the second one which displays second header, and the third one which displays the details.

Now when I click, the major grid which contains the two other grid is displayed twice.

I want my grid to be called only once.

just remove the OnRowDataBound="grdGrpPrntRpt_RowDataBound" and rest from the front end. Your problem will be solved
 
Share this answer
 
Comments
BlehBlahBah 16-Oct-13 2:39am    
doesn't help. instead it just hides my main grid text and doesn't find rest of the grid.
Hello Professional

Some time this problem occurs ..

Generally every Developer getting Affected by this.

Try to Just Remove the Specific Event where Problem Detecting and Create it Again

and below the link there is good discussion like this problem
Take a look may helps You with some your Own Clue ...


http://forums.asp.net/t/1002747.aspx[^]


Happy to Help!!
 
Share this answer
 
Comments
BlehBlahBah 16-Oct-13 2:53am    
i have already went through this page.. :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900