Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Unable to get ParentIndex :(

All the time it gives 0 value while I am Changing Text from Row 3

C#
public void txtChildQty_textChanged(object sender, EventArgs e)
{
    IsChildGridAdded = true;
    decimal QTY;
    int Parentindex;

    GridViewRow dgvChildDocuments = (GridViewRow)((TextBox)sender).Parent.Parent;
    Parentindex = dgvChildDocuments.RowIndex;

    decimal UnitPerPrice = Convert.ToDecimal(dgvChildDocuments.Cells[1].Text.Substring(1).ToString());
    TextBox t = (TextBox)dgvChildDocuments.FindControl("txtChildQty");
    if (t.Text == "")
    { QTY = 1; t.Text = "1"; }
    else
    {
        try { QTY = Convert.ToDecimal(t.Text); }
        catch { QTY = 0; t.Text = "1"; }
    }

    dgvChildDocuments.Cells[3].Text = "$" + (UnitPerPrice * QTY).ToString();

    ChildQty = TotalChildQty(Parentindex);
    
    BindTotalAmount();
}

XML
<asp:GridView runat="server" ID="dgvCartItems" AutoGenerateColumns="false" 
      CssClass="AccountBillinggrid" Visible="true"
      onrowcommand="dgvCartItems_RowCommand" 
      onrowdeleting="dgvCartItems_RowDeleting" 
        onrowdatabound="dgvCartItems_RowDataBound">
      <EmptyDataTemplate>
        <center><b style="color:Red">No Items In Shopping Cart</b></center>
      </EmptyDataTemplate>
      <Columns>
        <asp:BoundField HeaderText="Order ProductID" DataField="CartOrderProductID"/>
        <asp:TemplateField HeaderText="Item" >
          <ItemTemplate>
            <asp:Label runat="server" ID="lblproductTitleDisplay" Visible="false"></asp:Label>
            <asp:ImageButton CommandName="ColExp" CommandArgument='<%# Container.DataItemIndex %>' runat="server" ID="productCaseGroupIcon" ImageUrl="/images/icons/inline/expand.jpg" Visible="false" OnClick="productCaseGroupIcon_Click" style="width:auto" />
            <h1>
              <asp:Label runat="server" ID="lblCaseNumber" Visible="false"></asp:Label>
              <asp:ImageButton CommandName="NavigateToSummary" CommandArgument='<%# Container.DataItemIndex %>' runat="server" ID="ImgSummaryDetails" ImageUrl="/images/icons/inline/view.png" Visible="false" style="width:auto"  />
            </h1>
            <br />
            <font style="font-size:10px;">
              &nbsp;&nbsp;&nbsp;
              <em>
                
                <asp:Label runat="server" ID="lblPlaintiffandDefendant" Visible="false"></asp:Label>
              
              </em>
              
            </font>
            <br />&nbsp;&nbsp;&nbsp;
            <font style="font-size:10px;">
              <asp:Label runat="server" ID="lblClientCode" Visible="false" Text="Client Code/Notes:"></asp:Label>
            </font>
            <asp:TextBox runat="server" ID="txtNotesGroup" Visible="false" style="font-size:10px"></asp:TextBox>

           
            <asp:GridView runat="server" ID="dgvChildDocuments" AutoGenerateColumns="false" Visible="false" CssClass="AccountBillinggrid">
            <Columns>
            <asp:TemplateField>
            <ItemTemplate>
            <asp:ImageButton runat="server" ID="pdfDocumentsIcon" ImageUrl="/images/icons/inline/pdf.gif" />
            <font style="font-size:12px;"><asp:Label runat="server" ID="lblPDFDocuments"></asp:Label></font>
            </ItemTemplate>
            </asp:TemplateField>

            <asp:BoundField DataField="productOrderPrice" DataFormatString="${0}" />

            <asp:TemplateField>
          <ItemTemplate>
            <asp:TextBox runat="server" ID="txtChildQty" AutoPostBack="true" Text='<%# Eval("productQTY") %>' OnTextChanged="txtChildQty_textChanged"></asp:TextBox>
            <asp:ImageButton CommandName="DeleteChildItem" CommandArgument='<%# Container.DataItemIndex %>' runat="server" ID="imgChildDelete" ImageUrl="/images/icons/inline/cross.png" OnClick="imgChildDelete_Click" style="vertical-align:middle;" />
          </ItemTemplate>
        </asp:TemplateField>

        <asp:BoundField DataField="productOrderPrice" DataFormatString="${0}"/>

        <asp:TemplateField>
        <ItemTemplate>
        <asp:HiddenField runat="server" ID="hdnCaseDocumentID"/>
        </ItemTemplate>
        </asp:TemplateField>
            </Columns>
            </asp:GridView>

         
        </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="Unit Price" DataField="productOrderPrice" DataFormatString="${0}" />
        <asp:TemplateField HeaderText="Qty" ControlStyle-Width="30">
          <ItemTemplate>
            <asp:TextBox runat="server" ID="txtQty" AutoPostBack="true" OnTextChanged="txtQty_textChanged" Width="25px" Visible="false"></asp:TextBox>
            <asp:ImageButton CommandName="Delete" CommandArgument='<%# Container.DataItemIndex %>' runat="server" ID="imgDelete" ImageUrl="/images/icons/inline/cross.png" Visible="false" style="vertical-align:middle; width:13px;" />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="Total" />
      </Columns>
    </asp:GridView>
Posted
v3
Comments
jeenamary 28-Sep-13 8:59am    
Can you please post your aspx page as well?
Afzal Shaikh 1-Oct-13 1:23am    
see added the aspx page

1 solution

Hello there,

Try changing this line
C#
GridViewRow dgvChildDocuments = (GridViewRow)((TextBox)sender).Parent.Parent;
to this
C#
GridViewRow dgvChildDocuments = (GridViewRow)((TextBox)sender).NamingContainer
and if
C#
dgvChildDocuments.RowIndex
is still zero, then also try
C#
dgvChildDocuments.DataItemIndex


Hope it helps.

Good luck

Azee
 
Share this answer
 
Comments
Afzal Shaikh 1-Oct-13 2:18am    
With naming container It gives index of child row and with Parent.Parent ,it too gives child rowindex.

I mean to say When i change text from 2nd row of child gridview inside 1st row of Parent , it returns child 2=rowindex
instead of 1=rowindex

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