Click here to Skip to main content
16,019,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my child grid(inner grid)

protected void ParameterGv_RowCommand(object sender, GridViewCommandEventArgs e)
{
  GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
  GridView gv = (GridView)(e.CommandSource);
  //string s = gv.DataKeys[row.RowIndex][0].ToString();
  int i = Convert.ToInt32(e.CommandArgument);
  GridViewRow Childrow = gv.Rows[i];
  if (e.CommandName == "Edit")
  {
               
  }
}
protected void ParameterGv_RowEditing(object sender, GridViewEditEventArgs e)
{
  GridView gvtemp = (GridView)sender;
  gvtemp.EditIndex = e.NewEditIndex;
      
  FillmainGrid();
}

edit row command firing but update cancel is not coming again edit is comming


XML
<asp:GridView ID="HeaderGv" runat="server" AutoGenerateColumns="False" OnRowDataBound="HeaderGv_RowDataBound" Width="100%" DataKeyNames="PARAM_HEAD_ID" ShowFooter="true">
                   <Columns>
                       <asp:TemplateField HeaderText="SNO">
                           <ItemTemplate>
                               <asp:Label ID="SNOLbl" runat="server" Text='<%#Eval("SNO") %>'></asp:Label>
                           </ItemTemplate>
                           <FooterTemplate>
                               <asp:Button ID="ADDBtn" runat="server" onclick="ADDBtn_Click" Text="ADD" />
                           </FooterTemplate>
                       </asp:TemplateField>
                       <asp:TemplateField HeaderText="Group">
                           <ItemTemplate>
                               <asp:Label ID="GroupLbl" runat="server" Text='<%# Eval("PARAM_HEAD_NAME") %>'></asp:Label>
                               <asp:GridView ID="ParameterGv" runat="server" Width="100%" OnRowDataBound="ParameterGv_RowDataBound"
                                   HeaderStyle-CssClass="grid_col_head" AutoGenerateColumns="False"
                                   onrowcancelingedit="ParameterGv_RowCancelingEdit"
                                   onrowediting="ParameterGv_RowEditing"
                                   onrowupdating="ParameterGv_RowUpdating" DataKeyNames="PARAM_SUB_ID"
                                   onrowcommand="ParameterGv_RowCommand">
                                   <Columns>
                                       <asp:BoundField DataField="SNO" ItemStyle-Width="15" HeaderText="SNO" >
                                           <ItemStyle Width="15px" />
                                       </asp:BoundField>
                                       <asp:BoundField DataField="PARAM_HEAD_ID" HeaderText="PARAM ID" />
                                       <asp:BoundField DataField="PARAM_SUB_ID" HeaderText="PARAMSUBID" />
                                       <asp:BoundField DataField="PARAM_SUB_NAME" HeaderText="PARAM SUB NAME"  ReadOnly="false"/>
                                       <asp:TemplateField HeaderText="Results Of Verification">
                                           <ItemTemplate>
                                               <asp:DropDownList ID="ResultsDdl" runat="server">
                                                   <asp:ListItem>--select--</asp:ListItem>
                                                   <asp:ListItem>yes</asp:ListItem>
                                                   <asp:ListItem>No</asp:ListItem>
                                                   <asp:ListItem>NA</asp:ListItem>
                                               </asp:DropDownList>
                                           </ItemTemplate>
                                           <EditItemTemplate>
                                               <asp:DropDownList ID="ResultsDdl" runat="server">
                                                   <asp:ListItem>--select--</asp:ListItem>
                                                   <asp:ListItem>yes</asp:ListItem>
                                                   <asp:ListItem>No</asp:ListItem>
                                                   <asp:ListItem>NA</asp:ListItem>
                                               </asp:DropDownList>
                                           </EditItemTemplate>
                                       </asp:TemplateField>
                                       <asp:TemplateField HeaderText="Details oF Audit Findings">
                                           <ItemTemplate>
                                               <asp:DropDownList ID="FindingsDdl" runat="server">
                                                   <asp:ListItem>--select--</asp:ListItem>
                                                   <asp:ListItem>yes</asp:ListItem>
                                                   <asp:ListItem>No</asp:ListItem>
                                                   <asp:ListItem>NA</asp:ListItem>
                                               </asp:DropDownList>
                                           </ItemTemplate>
                                           <EditItemTemplate>
                                               <asp:DropDownList ID="FindingsDdl" runat="server">
                                                   <asp:ListItem>--select--</asp:ListItem>
                                                   <asp:ListItem>yes</asp:ListItem>
                                                   <asp:ListItem>No</asp:ListItem>
                                                   <asp:ListItem>NA</asp:ListItem>
                                               </asp:DropDownList>
                                           </EditItemTemplate>
                                       </asp:TemplateField>
                                       <asp:TemplateField HeaderText="Rectified by Audit Team">
                                           <ItemTemplate>
                                               <asp:DropDownList ID="RectifiedDdl" runat="server">
                                                   <asp:ListItem>--select--</asp:ListItem>
                                                   <asp:ListItem>yes</asp:ListItem>
                                                   <asp:ListItem>No</asp:ListItem>
                                                   <asp:ListItem>NA</asp:ListItem>
                                               </asp:DropDownList>
                                           </ItemTemplate>
                                           <EditItemTemplate>
                                               <asp:DropDownList ID="RectifiedDdl" runat="server">
                                                   <asp:ListItem>--select--</asp:ListItem>
                                                   <asp:ListItem>yes</asp:ListItem>
                                                   <asp:ListItem>No</asp:ListItem>
                                                   <asp:ListItem>NA</asp:ListItem>
                                               </asp:DropDownList>
                                           </EditItemTemplate>
                                       </asp:TemplateField>
                                       <asp:CommandField HeaderText="Edit" ShowEditButton="true"/>
                                   </Columns>
                                   <HeaderStyle CssClass="grid_col_head" />
                               </asp:GridView>
                           </ItemTemplate>
                           <EditItemTemplate>
                               <asp:DropDownList ID="ResultsoneDdl" runat="server">
                                   <asp:ListItem>--Select--</asp:ListItem>
                                   <asp:ListItem>Yes</asp:ListItem>
                                   <asp:ListItem>NO</asp:ListItem>
                                   <asp:ListItem>N/A</asp:ListItem>
                               </asp:DropDownList>
                           </EditItemTemplate>
                       </asp:TemplateField>
                       <asp:BoundField DataField="PARAM_HEAD_ID" HeaderText="HeaderID"
                           ReadOnly="True" />
                   </Columns>
               </asp:GridView>


public partial class AuditCheckList : System.Web.UI.Page
  {
      DataTable ForDdl;
      #region Logger Setup
      protected static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
      #endregion
      //Creating an object for datacontainer class
      DCCheckList objdc = new DCCheckList();
      //creating an  object for business layer class
      BLCkeckList objbl = new BLCkeckList();
      protected void Page_Load(object sender, EventArgs e)
      {
          Session["UserId"] = 101;
          Session["RoleId"] = 201;
          Session["RoleName"] = "admin";
          objdc.auditphase = Request.QueryString["AuditPhase"];
          objdc.auditorname = Request.QueryString["AuditorName"];
          objdc.branch = Request.QueryString["Branch"];
          objdc.ActualStartDate = Request.QueryString["ActualStartDate"];
          if (Session["UserId"] != null && Session["RoleId"] != null && Session["RoleName"] != null)
          {
              if (!IsPostBack)
              {
                  NameLbl.Text = Request.QueryString["AuditorName"];
                  DateLbl.Text = Request.QueryString["ActualStartDate"];
                  BranchLbl.Text = Request.QueryString["Branch"];
                  //This method binds data to the gridview
                  FillmainGrid();
              }
          }
          else
          {
              Session.Abandon();
              Response.Redirect("~/Login.aspx");
          }
      }
      private void FillmainGrid()
      {
          DataTable dt = new DataTable();
          dt = objbl.BindmainGrid();
          HeaderGv.DataSource = dt;
          HeaderGv.DataBind();
      }
      protected void HeaderGv_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          GridViewRow row = e.Row;
          if (e.Row.RowType != DataControlRowType.Pager)
          {
              if (e.Row.RowType == DataControlRowType.Header)
              {
                  e.Row.Cells[0].Visible = false;
                  e.Row.Cells[1].Visible = false;
                  e.Row.Cells[2].Visible = false;
              }
              if (e.Row.RowType == DataControlRowType.DataRow)
              {
                  e.Row.Cells[1].ForeColor = System.Drawing.Color.FromArgb(153, 0, 0);
                  e.Row.Cells[1].BackColor = Color.FromArgb(221, 221, 221);
                  e.Row.Cells[1].Font.Size = 12;
                  e.Row.Cells[1].Font.Name = "Century";
                  e.Row.Cells[1].Visible = true;
                  e.Row.Cells[0].Visible = false;
                  e.Row.Cells[2].Visible = false;
              }
          }
          if (row.DataItem == null)
          {
              return;
          }
          GridView gv = new GridView();
          objdc.headerID = Convert.ToInt32(e.Row.Cells[2].Text);
          gv = (GridView)row.Cells[1].FindControl("ParameterGv");
          if (gv != null)
          {
              BindCheckList(gv, objdc.headerID);
          }
      }
      private void BindCheckList(GridView gv, int p)
      {
          objdc.auditphase = Request.QueryString["AuditPhase"];
          objdc.auditorname = Request.QueryString["AuditorName"];
          objdc.branch = Request.QueryString["Branch"];
          objdc.headerID = p;
          ViewState["HeaderId"] = p;
          DataTable dt = new DataTable();
          dt = objbl.FillParametersGrid(ref objdc);
          DataTable dt1 = new DataTable();
          dt1 = objbl.getdata(ref objdc);
          if (dt1.Rows.Count > 0)
          {
              ForDdl = dt1.Copy();
              ForDdl.Columns.Remove("SNO");
              ForDdl.Columns.Remove("PARAM_HEAD_ID");
              ForDdl.Columns.Remove("PARAM_SUB_ID");
              ForDdl.Columns.Remove("PARAM_SUB_NAME");

              gv.DataSource = dt1;
              gv.DataBind();
          }
          else
          {
              gv.DataSource = dt;
              gv.DataBind();
          }
      }
      protected void ParameterGv_RowDataBound(object sender, GridViewRowEventArgs e)
      {//grid row data bound
          GridViewRow row = e.Row;
          e.Row.Cells[0].BackColor = Color.White;
          e.Row.Cells[0].ForeColor = Color.Black;
          e.Row.Cells[0].Font.Bold = false;
          e.Row.Cells[0].Font.Size = 11;
          e.Row.Cells[0].Font.Name = "calibri";
          e.Row.Cells[3].BackColor = Color.White;
          e.Row.Cells[3].ForeColor = Color.Black;
          e.Row.Cells[3].Font.Bold = false;
          e.Row.Cells[3].Font.Size = 11;
          e.Row.Cells[3].Font.Name = "calibri";
          e.Row.Cells[4].BackColor = Color.White;
          e.Row.Cells[4].ForeColor = Color.Black;
          e.Row.Cells[4].Font.Bold = false;
          e.Row.Cells[4].Font.Size = 11;
          e.Row.Cells[4].Font.Name = "calibri";
          e.Row.Cells[5].BackColor = Color.White;
          e.Row.Cells[5].ForeColor = Color.Black;
          e.Row.Cells[5].Font.Bold = false;
          e.Row.Cells[5].Font.Size = 11;
          e.Row.Cells[5].Font.Name = "calibri";
          e.Row.Cells[6].BackColor = Color.White;
          e.Row.Cells[6].ForeColor = Color.Black;
          e.Row.Cells[6].Font.Bold = false;
          e.Row.Cells[6].Font.Size = 11;
          e.Row.Cells[6].Font.Name = "calibri";
          e.Row.Cells[7].BackColor = Color.White;
          e.Row.Cells[7].Font.Bold = false;
          e.Row.Cells[7].Font.Name = "calibri";
          if (e.Row.RowType == DataControlRowType.Header)
          {
              // e.Row.Cells[0].Visible = false;
              e.Row.Cells[1].Visible = false;
              e.Row.Cells[2].Visible = false;
              //e.Row.Cells[3].Visible = false;
              //e.Row.Cells[4].Visible = false;
              //if (ViewState["STATUS"] == "freezed")
              //{
              //    e.Row.Cells[7].Visible = false;
              //}
          }
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              e.Row.Cells[0].Visible = true;
              e.Row.Cells[1].Visible = false;
              e.Row.Cells[2].Visible = false;
              e.Row.Cells[3].Visible = true;
              e.Row.Cells[4].Visible = true;
              //code for drop downs binds in child grid
              ((DropDownList)row.Cells[4].FindControl("ResultsDdl")).Enabled = true;
              ((DropDownList)row.Cells[5].FindControl("FindingsDdl")).Enabled = true;
              ((DropDownList)row.Cells[6].FindControl("RectifiedDdl")).Enabled = true;
              DataRowView drv = (DataRowView)e.Row.DataItem;
              string Results = drv["RESULTSOFAUDIT"].ToString();
              string Findings = drv["AUDITFINDINGS"].ToString();
              string Rectified = drv["RECTIFICATIONS"].ToString();
              DropDownList Resulddl = (DropDownList)e.Row.FindControl("ResultsDdl");
              DropDownList Findddl = (DropDownList)e.Row.FindControl("FindingsDdl");
              DropDownList Rectddl = (DropDownList)e.Row.FindControl("RectifiedDdl");
              Resulddl.Items.FindByValue(Results).Selected = true;
              Findddl.Items.FindByValue(Findings).Selected = true;
              Rectddl.Items.FindByValue(Rectified).Selected = true;
              //if (ViewState["STATUS"].ToString() == "freezed")
              //{
              //    e.Row.Cells[7].Visible = false;
              //}

          }
      }
      protected void ParameterGv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
      {// child grid row cancel event
          e.Cancel = true;
          HeaderGv.EditIndex = -1;
          FillmainGrid();
      }
      protected void ParameterGv_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {//child grid row updating
          try
          {
              GridViewRow row = (GridViewRow)HeaderGv.Rows[e.RowIndex];
              GridView gv = (GridView)row.Cells[1].FindControl("ParameterGv");
              objdc.auditphase = Request.QueryString["AuditPhase"];
              objdc.auditorname = Request.QueryString["AuditorName"];
              objdc.branch = Request.QueryString["Branch"];
              //objdc.headname = HeaderGv.Rows[e.RowIndex].Cells[1].Text;
              objdc.parameterid = Convert.ToInt32(gv.Rows[e.RowIndex].Cells[2].Text);
              objdc.headerID = Convert.ToInt32(gv.Rows[e.RowIndex].Cells[1].Text);
              //objdc.paramsubname = gv.Rows[e.RowIndex].Cells[3].Text;
              //objdc.headname = ((Label)HeaderGv.Rows[e.RowIndex].Cells[1].FindControl("GroupLbl")).Text;
              objdc.results = ((DropDownList)gv.Rows[e.RowIndex].Cells[4].FindControl("ResultsDdl")).SelectedItem.Text;
              objdc.findings = ((DropDownList)gv.Rows[e.RowIndex].Cells[5].FindControl("FindingsDdl")).SelectedItem.Text;
              objdc.Rectifications = ((DropDownList)gv.Rows[e.RowIndex].Cells[6].FindControl("RectifiedDdl")).SelectedItem.Text;
              objdc.modifiedby = Session["RoleName"].ToString();
              int result = objbl.updatechecklist(ref objdc);
              if (result == 1)
              {
                  Literal li = new Literal();
                  FillmainGrid();
                  li.Text = "<script>alert('Data Updated sucessfully');</script>";
                  Page.Controls.Add(li);
              }
          }
          catch (Exception ex)
          {
              log.Error(ex.Message, ex);
          }
      }
      protected void ParameterGv_RowEditing(object sender, GridViewEditEventArgs e)
      {//child grid rowedit event
           FillmainGrid();
          GridView gvtemp = (GridView)sender;
          gvtemp.EditIndex = e.NewEditIndex;


      }
      protected void ParameterGv_RowCommand(object sender, GridViewCommandEventArgs e)
      {//Child Grid rowcommand
          GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
          GridView gv = (GridView)(e.CommandSource);
          //string s = gv.DataKeys[row.RowIndex][0].ToString();
          int i = Convert.ToInt32(e.CommandArgument);
          GridViewRow Childrow = gv.Rows[i];
          if (e.CommandName == "Edit")
          {

          }
      }
      protected void ADDBtn_Click(object sender, EventArgs e)
      {
          try
          {
              int j = 0;
              for (int i = 0; i < HeaderGv.Rows.Count; i++)
              {
                  GridView gv = (GridView)HeaderGv.Rows[i].Cells[1].FindControl("ParameterGv");
                  for (int k = 0; k < gv.Rows.Count; k++)
                  {
                      objdc.auditphase = Request.QueryString["AuditPhase"];
                      objdc.auditorname = Request.QueryString["AuditorName"];
                      objdc.branch = Request.QueryString["Branch"];
                      objdc.parameterid = Convert.ToInt32(gv.Rows[k].Cells[2].Text);
                      objdc.headerID = Convert.ToInt32(gv.Rows[k].Cells[1].Text);
                      objdc.paramsubname = gv.Rows[k].Cells[3].Text;
                      objdc.headname = ((Label)HeaderGv.Rows[i].Cells[1].FindControl("GroupLbl")).Text;
                      objdc.results = ((DropDownList)gv.Rows[k].Cells[4].FindControl("ResultsDdl")).SelectedItem.Text;
                      objdc.findings = ((DropDownList)gv.Rows[k].Cells[5].FindControl("FindingsDdl")).SelectedItem.Text;
                      objdc.Rectifications = ((DropDownList)gv.Rows[k].Cells[6].FindControl("RectifiedDdl")).SelectedItem.Text;
                      objdc.insertedby = Session["RoleName"].ToString();
                      objdc.modifiedby = Session["RoleName"].ToString();
                      objbl.insertchecklist(ref objdc);
                  }
                  j = j + 1;
              }
              if (j == HeaderGv.Rows.Count)
              {
                  Literal li = new Literal();
                  FillmainGrid();
                  HeaderGv.ShowFooter = false;
                  objbl.CloseAuditChecklist(ref objdc);
                  li.Text = "<script>alert('Data Saved Sucssfully');</script>";
                  Page.Controls.Add(li);
              }
          }
          catch (Exception ex)
          {
              //Literal li = new Literal();
              //li.Text = "<script>alert('"+ex.Message+"');</script>";
              //Page.Controls.Add(li);
              log.Error(ex.Message, ex);
          }
      }
      protected void FreezeBtn_Click(object sender, EventArgs e)
      {
          objdc.auditphase = Request.QueryString["AuditPhase"];
          objdc.auditorname = Request.QueryString["AuditorName"];
          objdc.branch = Request.QueryString["Branch"];
          objdc.modifiedby = Session["RoleName"].ToString();
          objdc.formname = "Check List";
          objdc.headername = "Content Of Report";
          int result = objbl.UpdateStatus(ref objdc);
          if (result == 1)
          {
              ViewState["STATUS"] = "freezed";
              FillmainGrid();
              Literal li = new Literal();
              li.Text = "<script>alert('form freezed sucessfully');</script>";
              FreezeBtn.Visible = false;
              Page.Controls.Add(li);
          }
          else
          {
              Literal li = new Literal();
              li.Text = "<script>alert('Please close this form before freezing it');</script>";
              Page.Controls.Add(li);
          }
      }
  }
Posted
Updated 17-Nov-10 18:28pm
v2
Comments
JF2015 18-Nov-10 0:29am    
This is too much code. You will hardly find someone who is going to check all this source. Try to break down the code to the point that is causing the issue and explain a little more what is not working.

1 solution

It would be better for you to have a look at this article:
Editable Nested GridView (All-in-One)[^]

This has the functionality you are looking for.
 
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