Click here to Skip to main content
15,910,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone can any one help me out how to loop through gridview rows which is another gridview here my task is to loop through each row of inner grid and need to update the records with the value in the textbox inside the inner grid here is my code

C#
protected void btnupdateregistry_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = new DataTable();
            dt = WCFService.Host_Getstillneedsquantity(Session["hunqid"].ToString());
             //First grid
            foreach (DataListItem item12 in dMainCategory.Items)
            {
                DataList repHotDeals = (DataList)item12.FindControl("repHotDeals");

                //DataList repHotDeals = sender as DataList;
                foreach (DataListItem item in repHotDeals.Items)
                {
                    ModelId = Convert.ToInt32(((HiddenField)item.FindControl("hfddlmodelid")).Value);
                    int prevQty = 0;
                    int prevStillneedsQty = 0; //nag written..
                   
                    DataRow[] dr = dt.Select("id=" + ModelId);
                    prevQty = Convert.ToInt32(dr[0]["Quantity"].ToString());
                    prevStillneedsQty = Convert.ToInt32(dr[0]["stillneedsQuantity"].ToString()); 

                    TextBox quantityrequired = ((TextBox)item.FindControl("txtquantity"));
                    ViewState["txtquant"] = quantityrequired.Text;

                    Label SellingPrice = (Label)item.FindControl("lblsellingprice");
                    Label lblamount = (Label)item.FindControl("lblamount");

                    if (quantityrequired.Text != "" && int.Parse(quantityrequired.Text) >= prevQty)
                    {                  
                        int PurchasedQty = prevQty - prevStillneedsQty;                                             
                        int diffQnty = Convert.ToInt32(quantityrequired.Text) - prevQty;
                        int NewStillNeedsQnty = diffQnty + prevStillneedsQty;
                        WCFService.UpdateQuantity1(ModelId, Session["hunqid"].ToString(), Convert.ToInt32(quantityrequired.Text), NewStillNeedsQnty);

                    }
                    else
                    {
                        lblordsum.Visible = true;
                        lblordsum.Text = "Quantity Should Not Less than Purchased Quantity or Zero";
                        lblordsum.ForeColor = System.Drawing.Color.Red;
                    }
                    
                }
                
            }
            GetHostId("None");
            lblordsum.Text = "Requested Quantity is updated";
            lblordsum.Visible = true;
            lblordsum.ForeColor = System.Drawing.Color.Green;
        }
        catch (Exception ex)
        {
            WCFService.User_LogError("HostCollections", "lbtnAddtoCart_Click", (ex.InnerException != null) ? ex.InnerException.ToString() : ex.Message.ToString(), 4, 4);
        }
    }
Posted
Updated 28-Aug-13 19:27pm
v2
Comments
VishwaKL 29-Aug-13 2:01am    
Find the secondary gridview as find control in first gridview event, there u can loop through the second gridview

1 solution

For looping through the inner grid, you can use the below code (when clicking on a button)


gvMainGrid is the parent grid
gvInnerGrid is the child grid

Using the below code you can loop through any controls.

C#
protected void btnUpdate_Click(object sender, EventArgs e)
       {
           for (int i = 0; i < gvMainGrid.Rows.Count; i++)
           {
               for (int j = 0; j < ((GridView)gv.Rows[i].FindControl("gvInnerGrid")).Rows.Count; j++)
               {
                   ((TextBox)((GridView)gvMainGrid.Rows[i].FindControl("gvInnerGrid")).Rows[j].FindControl("txtSNo")).Text = "100";

               }
           }
       }



Hope this will help you.
 
Share this answer
 

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