c# - How to Add Dynamic Text Box inside child gridview in nested gridview? -
how add dynamic text box inside child gridview ?below screen shot give clear picture of question.i unable add on second row shown in screen shot.
thank in advance
adding dynamic rows in gridview textboxes
<asp:gridview id="gridview1" runat="server" showfooter="true" autogeneratecolumns="false"> <columns> <asp:boundfield datafield="rownumber" headertext="row number" /> <asp:templatefield headertext="header 1"> <itemtemplate> <asp:textbox id="textbox1" runat="server"></asp:textbox> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="header 2"> <itemtemplate> <asp:textbox id="textbox2" runat="server"></asp:textbox> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="header 3"> <itemtemplate> <asp:textbox id="textbox3" runat="server"></asp:textbox> </itemtemplate> <footerstyle horizontalalign="right" /> <footertemplate> <asp:button id="buttonadd" runat="server" text="add new row" /> </footertemplate> </asp:templatefield> </columns> </asp:gridview>
your binding code
private void setinitialrow(){ datatable dt = new datatable(); datarow dr = null; dt.columns.add(new datacolumn("rownumber", typeof(string))); dt.columns.add(new datacolumn("column1", typeof(string))); dt.columns.add(new datacolumn("column2", typeof(string))); dt.columns.add(new datacolumn("column3", typeof(string))); dr = dt.newrow(); dr["rownumber"] = 1; dr["column1"] = string.empty; dr["column2"] = string.empty; dr["column3"] = string.empty; dt.rows.add(dr); //store datatable in viewstate viewstate["currenttable"] = dt; gridview1.datasource = dt; gridview1.databind(); }
page load event code
if (!page.ispostback){ setinitialrow(); }
running codes above give output below:
method generating rows when clicking button
private void addnewrowtogrid(){ int rowindex =0; if (viewstate["currenttable"] != null) { datatable dtcurrenttable = (datatable)viewstate["currenttable"]; datarow drcurrentrow = null; if (dtcurrenttable.rows.count > 0) { (int = 1; <= dtcurrenttable.rows.count; i++) { //extract textbox values textbox box1 = (textbox)gridview1.rows[rowindex].cells[1].findcontrol("textbox1"); textbox box2 = (textbox)gridview1.rows[rowindex].cells[2].findcontrol("textbox2"); textbox box3 = (textbox)gridview1.rows[rowindex].cells[3].findcontrol("textbox3"); drcurrentrow = dtcurrenttable.newrow(); drcurrentrow["rownumber"] = + 1; drcurrentrow["column1"] = box1.text; drcurrentrow["column2"] = box2.text; drcurrentrow["column3"] = box3.text; rowindex++; } //add new row datatable dtcurrenttable.rows.add(drcurrentrow); //store current data viewstate viewstate["currenttable"] = dtcurrenttable; //rebind grid current data gridview1.datasource = dtcurrenttable; gridview1.databind(); } } else { response.write("viewstate null"); } //set previous data on postbacks setpreviousdata(); }
setting previous data
private void setpreviousdata(){ int rowindex = 0; if (viewstate["currenttable"] != null) { datatable dt = (datatable)viewstate["currenttable"]; if (dt.rows.count > 0) { (int = 1; < dt.rows.count; i++) { textbox box1 = (textbox)gridview1.rows[rowindex].cells[1].findcontrol("textbox1"); textbox box2 = (textbox)gridview1.rows[rowindex].cells[2].findcontrol("textbox2"); textbox box3 = (textbox)gridview1.rows[rowindex].cells[3].findcontrol("textbox3"); box1.text = dt.rows[i]["column1"].tostring(); box2.text = dt.rows[i]["column2"].tostring(); box3.text = dt.rows[i]["column3"].tostring(); rowindex++; } } } }
button click event code
protected void buttonadd_click(object sender, eventargs e){ addnewrowtogrid(); }
for detail tutorial visit : geekswithblock
Comments
Post a Comment