Forums - C# Corner

Forum guidelines
Srinivas Bh

Srinivas Bh

  • 1.5k
  • 5
  • 469

I want to add rows to child grid on a button click

Apr 21 2017 6:12 AM
by using the add button im trying to add rows to child grid view like this   
protected void Add(object sender, EventArgs e)
{
GridViewRow row = (GridViewRow)((ImageButton)sender).Parent.Parent;
GridView gv = (GridView)row.FindControl("Gridview1");
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("EmployeeKey", typeof(int)));
dt.Columns.Add(new DataColumn("tbName", typeof(string)));
dt.Columns.Add(new DataColumn("tbAmount", typeof(string)));
dt.Columns.Add(new DataColumn("tbEffDate", typeof(string)));
dr = dt.NewRow();
dr["EmployeeKey"] = gr.Cells[1].Text;
dr["tbName"] = string.Empty;
dr["tbAmount"] = string.Empty;
dr["tbEffDate"] = string.Empty;
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
gv.DataSource = dt;
gv.DataBind();
}
 
 
but its not working out child grid is not updating with rows can any body help 
 
 
here is the source code
 
<asp:GridView ID="gvEmpDetails" runat="server" AutoGenerateColumns="False"
ShowHeader="true" DataKeyNames="EmployeeKey" AllowPaging="true"
PageSize="5" ForeColor="#333333" GridLines="None"
onpageindexchanging="gvEmpDetails_PageIndexChanging"
Width="80%" OnRowDataBound="gvEmpDetails_RowDataBound" OnRowCommand="gvEmpDetails_RowCommand" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField ItemStyle-BorderStyle="None" HeaderText="S.No" ItemStyle-Font-Bold="true">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmployeeKey" HeaderText="EmployeeKey" HtmlEncode="true" />
<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" HtmlEncode="true" />
<asp:BoundField DataField="EmpName" HeaderText="Employee Name" HtmlEncode="true" />
<asp:TemplateField ItemStyle-BorderStyle="None" HeaderText="Add" ItemStyle-Font-Bold="true">
<ItemTemplate>
<asp:ImageButton ID="ibAdd" OnClick="Add" ImageUrl="~/Images/6-32.png" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<table style="width:100%">
<tr>
<td>Name</td>
<td>Amount</td>
<td>Effective Date</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<asp:GridView ID="gvEmpAllDetails" runat="server" ShowHeader="false" Width="70%"
ForeColor="#333333" GridLines="None" AutoGenerateColumns="false">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField ItemStyle-Horizontal>
<ItemTemplate>
<asp:TextBox ID="tbName" Text='<%# Eval("tbName") %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Horizontal>
<ItemTemplate>
<asp:TextBox ID="tbAmount" Text='<%# Eval("tbAmount") %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Horizontal>
<ItemTemplate>
<asp:TextBox ID="tbEffDate" Text='<%# Eval("tbEffDate") %>' onkeypress="return false" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="ceEffDate" TargetControlID="tbEffDate" Format="dd-MMM-yyyy" runat="server"></asp:CalendarExtender>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:ImageButton ID="ibRemove" ImageUrl="~/Images/list_remove.png" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" Horizontal ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" Horizontal ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
 
 

Answers (1)