Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi

I have created the nested gridview, parent and child.
Now I want to clone the child gridview after clicking on button. It should be cloned in same parent gridview.

Child gridview has the rowbind function, that should be applied to cloned grid

Is there any way to do this, Please Help Me.

Bellow is the code that i have tried


C#

protected void Button1_Click(Object sender, CommandEventArgs e)
{
string Partid = e.CommandArgument.ToString();
//datakey is assigned on grid control as DataKeyNames="UnivercityCode"
// string id1 = universityGrid.DataKeys[e.Row.RowIndex].Value.ToString();
//data table cloing
GridView gr1 = new GridView();
gr1.ID = "gvattrgrid";
gr1.HeaderStyle.CssClass = "attheaderstyle";
gr1.RowStyle.CssClass = "attrowstyle";
gr1.AlternatingRowStyle.CssClass = "attalterrowstyle";
gr1.CssClass = "attgridwidth";
gr1.CellPadding = 8;
gr1.AutoGenerateColumns = false;
gr1.CellSpacing = 10;
Table t = new Table();
gr1.Controls.Add(t);
foreach (GridViewRow row in gvPartGrid.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{

GridView gvChild = (GridView)row.FindControl("gvattributeGrid");

DataTable dt = new DataTable();
for (int i = 0; i < gvChild.Columns.Count; i++)
{
dt.Columns.Add("column" + i.ToString());
}
foreach (GridViewRow row2 in gvChild.Rows)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < gvChild.Columns.Count; j++)
{
dr["column" + j.ToString()] = row2.Cells[j].Text;
}

dt.Rows.Add(dr);
}

DataTable dtClone = dt.Clone();
gr1.DataSource = dtClone;
gr1.DataBind();
}
}
TextBox txtSearch = new TextBox();
txtSearch.ID = "txtSearch";
for (int i = 0; i < gvPartGrid.Rows.Count; i++)
{
//TextBox txtqty = (TextBox)gvPartGrid.Rows[i].FindControl("txtqty");
PlaceHolder appendgrids2 = gvPartGrid.Rows[i].FindControl("PlaceHolder1") as PlaceHolder;

string partid = gvPartGrid.Rows[i].Cells[1].Text;

if (partid == Partid)
{
appendgrids2.Controls.Add(txtSearch);
appendgrids2.Controls.Add(gr1);
}
}

}

Asp.net

XML
<div id="form1" >
                                                                              <div >
                                                                                  <%-- <asp:UpdatePanel ID="UpdatePanel14" runat="server">
                                                                                   <ContentTemplate >--%>
                                                                                   <asp:UpdatePanel ID="UpdatePanel14" runat="server" UpdateMode="Conditional">
                                                                                                      <ContentTemplate >
                                                                                  <asp:GridView ID="gvPartGrid" runat="server" DataKeyNames="part_id" Width="99%"
                                                                                  AutoGenerateColumns="False" OnRowDataBound="gvUserInfo_RowDataBound" GridLines="None" class="table" enable="false">
                                                                                      <HeaderStyle   />
                                                                                      <RowStyle CssClass="gridheader"  />
                                                                                      <AlternatingRowStyle BackColor="White" />

                                                                                      <HeaderStyle  />
                                                                                      <Columns>


                                                                                         <asp:TemplateField>
                                                                                          <HeaderTemplate>
                                                                                       <asp:CheckBox ID="checkallpart" AutoPostBack="true" OnCheckedChanged="chkAllpart_CheckedChanged" runat="server"  />

                                                                                               </HeaderTemplate>
                                                                                              <ItemTemplate>

                                                                                       <asp:CheckBox ID="chkAll" AutoPostBack="true" OnCheckedChanged="chkAll_CheckedChanged" runat="server"  class="flat-grey" />


                                                                                         </ItemTemplate>
                                                                                           <ItemStyle Width="20px" ></ItemStyle>
                                                                                          </asp:TemplateField>

                                                                                          <asp:BoundField DataField="part_id" HeaderText="PartId" HeaderStyle-HorizontalAlign="Left" >
                                                                                              <ItemStyle CssClass="hidden"></ItemStyle>
                                                                                              <HeaderStyle HorizontalAlign="Left" CssClass="hidden"></HeaderStyle>
                                                                                           </asp:BoundField>
                                                                                          <asp:BoundField DataField="part_name" HeaderText="Check All Parts" HeaderStyle-HorizontalAlign="Left" >
                                                                                              <HeaderStyle HorizontalAlign="Left" Width="150px" ></HeaderStyle>

                                                                                          </asp:BoundField>

                                                                                          <asp:TemplateField ItemStyle-Width="20px"  >
                                                                                              <ItemTemplate>
                                                                                                  <a href="JavaScript:divexpandcollapse('div<%# Eval("part_id") %>');" class="btn btn-primary" ><i id="imgdiv<%# Eval("part_id") %>" class="fa fa-arrow-circle-up"></i></a>
                                                                                                  <%--<a href="JavaScript:divexpandcollapse('div<%# Eval("part_id") %>');">
                                                                                                  <img id="imgdiv<%# Eval("part_id") %>" alt="sdfsdf" width="9px" border="0" src="plus.gif" />
                                                                                                  </a>--%>
                                                                                              </ItemTemplate>

                                                                                              <ItemStyle Width="952px" CssClass="no-padding" HorizontalAlign="Right" ></ItemStyle>
                                                                                          </asp:TemplateField>

                                                                                          <asp:TemplateField>
                                                                                              <ItemTemplate>
                                                                                                  <tr  >

                                                                                                      <td colspan="8" >
                                                                                                          <div id="div22<%# Eval("part_id") %>" ></div>
                                                                                                          <asp:TextBox ID="attributeappendedid" runat="server"></asp:TextBox>
                                                                                                          <div style="padding-top: 10px; padding-bottom:10px" >
                                                                                                          <div id="div<%# Eval("part_id") %>" style="display: none; position: relative; left: 15px; overflow: auto">
                                                                                                              <div id="attdatadiv">
                                                                                                             <div style="padding-bottom:15px" id="partquantitydiv">Quantity : <asp:TextBox ID="txtqty" runat="server"></asp:TextBox></div>
                                                                                                                  <asp:UpdatePanel ID="UpdatePanel12" runat="server" UpdateMode="Conditional">
                                                                                                                      <ContentTemplate >
                                                                                                               <asp:GridView ID="gvattributeGrid" runat="server" AutoGenerateColumns="false" class="table table-bordered" Width="98%" OnRowDataBound="gvattributeGrid_rowbind">
                                                                                                                  <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                                                                                                                  <RowStyle BackColor="#E1E1E1" />
                                                                                                                  <AlternatingRowStyle BackColor="White" />
                                                                                                                  <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                                                                                                                  <Columns>
                                                                                                                      <asp:TemplateField>
                                                                                                                          <ItemTemplate>
                                                                                                                              <asp:CheckBox ID="chkCheck" runat="server" class="flat-grey"  OnCheckedChanged="Orders_OnCheckedChanged" />
                                                                                                                          </ItemTemplate>
                                                                                                                      </asp:TemplateField>
                                                                                                                       <asp:TemplateField HeaderText="ID" SortExpression="attribute_id" >

                                                                                                                          <ItemTemplate>
                                                                                                                              <asp:Label  ID="lblattributeid" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"attribute_id") %>'></asp:Label>
                                                                                                                          </ItemTemplate>
                                                                                                                      </asp:TemplateField>
                                                                                                                       <asp:TemplateField HeaderText="Attribute Name" SortExpression="attribute_name" >

                                                                                                                          <ItemTemplate>
                                                                                                                              <asp:Label ID="lblattributename" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"attribute_name") %>'></asp:Label>
                                                                                                                          </ItemTemplate>
                                                                                                                      </asp:TemplateField>
                                                                                                                       <asp:TemplateField HeaderText="PART ID" SortExpression="part_id" >

                                                                                                                          <ItemTemplate>
                                                                                                                              <asp:Label ID="lblpartid" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"part_id") %>'></asp:Label>
                                                                                                                          </ItemTemplate>
                                                                                                                           <ItemStyle CssClass="hidden"></ItemStyle>
                                                                                                                          <HeaderStyle HorizontalAlign="Left" CssClass="hidden"></HeaderStyle>
                                                                                                                      </asp:TemplateField>
                                                                                                                       <asp:TemplateField HeaderText="Type" SortExpression="type" >

                                                                                                                          <ItemTemplate>
                                                                                                                              <asp:Label ID="lbltype" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"type") %>'></asp:Label>
                                                                                                                          </ItemTemplate>
                                                                                                                           <ItemStyle CssClass="hidden"></ItemStyle>
                                                                                                                          <HeaderStyle HorizontalAlign="Left" CssClass="hidden"></HeaderStyle>
                                                                                                                      </asp:TemplateField>
                                                                                                                       <asp:TemplateField HeaderText="Default Value" SortExpression="default_value" >

                                                                                                                          <ItemTemplate>
                                                                                                                              <asp:Label ID="lbldefaultvalue" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"default_value") %>'></asp:Label>
                                                                                                                              <asp:DropDownList ID="DropDownListvalues" runat="server" Visible="false"></asp:DropDownList>
                                                                                                                               </ItemTemplate>
                                                                                                                      </asp:TemplateField>
                                                                                                                      <asp:TemplateField HeaderText="Check For Other" >
                                                                                                                          <ItemTemplate>

                                                                                                                                  <asp:CheckBox ID="chkother" AutoPostBack="true" runat="server" class="flat-grey" OnCheckedChanged="chkother_OnCheckedChanged" />

                                                                                                                          </ItemTemplate>
                                                                                                                      </asp:TemplateField>
                                                                                                                      <asp:TemplateField HeaderText="Othe Value" >
                                                                                                                          <ItemTemplate>
                                                                                                                           <asp:UpdatePanel ID="UpdatePanel11" runat="server" UpdateMode="Conditional">
                                                                                                                              <ContentTemplate >
                                                                                                                                   <asp:TextBox ID="txtothervalue" runat="server" Visible="false"></asp:TextBox>

                                                                                                                               </ContentTemplate>
                                                                                                                             <Triggers>
                                                                                                                                 <asp:AsyncPostBackTrigger ControlID="chkother" EventName="CheckedChanged" />
                                                                                                                             </Triggers>
                                                                                                                           </asp:UpdatePanel>
                                                                                                                          </ItemTemplate>
                                                                                                                      </asp:TemplateField>

                                                                                                                  </Columns>
                                                                                                              </asp:GridView>
                                                                                                              </ContentTemplate>
                                                                                                                             <Triggers>
                                                                                                                                 <asp:AsyncPostBackTrigger ControlID="chkAll" EventName="CheckedChanged" />
                                                                                                                             </Triggers>
                                                                                                                           </asp:UpdatePanel>
                                                                                                                  <div id="appendedgrid<%# Eval("part_id") %>">
                                                                                                                       <%--<div style="padding-bottom:15px" id="partquantityappdiv">
                                                                                                                      </div>
                                                                                                                  <div id="appendedattgrid">
                                                                                                                      </div>--%>

                                                                                                              </div>
                                                                                                               <asp:UpdatePanel ID="UpdatePanel16" runat="server" UpdateMode="Conditional">
                                                                                                                   <ContentTemplate >
                                                                                                              <div style="float:right">
                                                                                                                  <%--<asp:Button ID="appgrid" runat="server" CssClass="btn btn-green"  Text="ADD" OnClick="Button1_Click" />--%>
                                                                                                                 <asp:Button ID="appgrid" runat="server" CssClass="btn btn-green"  Text="ADD" CommandArgument='<%# Eval("part_id") %>' OnCommand="Button1_Click" />
                                                                                                                  <%--<asp:Button ID="appgrid" runat="server" CssClass="btn btn-green"  Text="ADD" OnClientClick='<%# string.Format("javascript:return appendattgrid(\"{0},{1},{2}\")", gvattributeGrid.ClientID,Eval("part_id"),gvPartGrid.ClientID) %>'  />--%>
                                                                                                                <%-- <a href="JavaScript:appendattgrid('<%= appgrid.ClientID %>');" class="btn btn-green" >ADD</a>--%>

                                                                                                              </div>

                                                                                                                       <asp:PlaceHolder ID="PlaceHolder1" runat="server">


                                                                                                                       </asp:PlaceHolder>

                                                                                                                   </ContentTemplate>
                                                                                                                 <Triggers>
                                                                                                                       <asp:AsyncPostBackTrigger ControlID="appgrid" EventName="Click" />
                                                                                                                 </Triggers>
                                                                                                                  </asp:UpdatePanel>
                                                                                                                </div>



                                                                                                          </div>
                                                                                                              </div>


                                                                                                      </td>
                                                                                                  </tr>
                                                                                              </ItemTemplate>
                                                                                          </asp:TemplateField>
                                                                                      </Columns>
                                                                                  </asp:GridView>
                                                                                  </ContentTemplate>
                                                                                                         <Triggers>
                                                                                                               <asp:AsyncPostBackTrigger ControlID="drpsearchcategoryDownList1" EventName="SelectedIndexChanged" />
                                                                                                         </Triggers>
                                                                                                          </asp:UpdatePanel>
                                                                              <%-- </ContentTemplate>
                                                                                    </asp:UpdatePanel>--%>
                                                                              </div>

                                                                          </div>




Thanks
Posted
Updated 15-Jul-15 21:28pm
v2
Comments
Suvendu Shekhar Giri 10-Jul-15 1:22am    
Anything you have tried so far?
Member 11826979 10-Jul-15 2:30am    
I have tried with jquery but its not working properly, I want to clone it through c#
Member 11826979 10-Jul-15 2:32am    
I have tried trough c# but not able to clone and append to placeholder. I dont know what i m making mistake.Bellow is the code that i have tryed

protected void Button1_Click(Object sender, CommandEventArgs e)
{
string Partid = e.CommandArgument.ToString();
//datakey is assigned on grid control as DataKeyNames="UnivercityCode"
// string id1 = universityGrid.DataKeys[e.Row.RowIndex].Value.ToString();
//data table cloing
GridView gr1 = new GridView();
gr1.ID = "gvattrgrid";
gr1.HeaderStyle.CssClass = "attheaderstyle";
gr1.RowStyle.CssClass = "attrowstyle";
gr1.AlternatingRowStyle.CssClass = "attalterrowstyle";
gr1.CssClass = "attgridwidth";
gr1.CellPadding = 8;
gr1.AutoGenerateColumns = false;
gr1.CellSpacing = 10;
Table t = new Table();
gr1.Controls.Add(t);
foreach (GridViewRow row in gvPartGrid.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{

GridView gvChild = (GridView)row.FindControl("gvattributeGrid");

DataTable dt = new DataTable();
for (int i = 0; i < gvChild.Columns.Count; i++)
{
dt.Columns.Add("column" + i.ToString());
}
foreach (GridViewRow row2 in gvChild.Rows)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < gvChild.Columns.Count; j++)
{
dr["column" + j.ToString()] = row2.Cells[j].Text;
}

dt.Rows.Add(dr);
}

DataTable dtClone = dt.Clone();
gr1.DataSource = dtClone;
gr1.DataBind();
}
}
TextBox txtSearch = new TextBox();
txtSearch.ID = "txtSearch";
for (int i = 0; i < gvPartGrid.Rows.Count; i++)
{
//TextBox txtqty = (TextBox)gvPartGrid.Rows[i].FindControl("txtqty");
PlaceHolder appendgrids2 = gvPartGrid.Rows[i].FindControl("PlaceHolder1") as PlaceHolder;

string partid = gvPartGrid.Rows[i].Cells[1].Text;

if (partid == Partid)
{
appendgrids2.Controls.Add(txtSearch);
appendgrids2.Controls.Add(gr1);
}
}

}
[no name] 10-Jul-15 20:52pm    
Instead of creating dynamic grid control to clone, you can replace PlaceHolder control have another grid control which is Visible = false in your parent grid control, to hold clone data.

Now on your button click you just assign data and make it visible when it matching your condition of PartID for current row only. For this you need to maintain datasouse for parent grid control at common place e.g. page level variable.

It will be simple approach I think instead of having dynamic grid creation.
Member 11826979 11-Jul-15 1:06am    
Is it possible to clone more than one

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