asp.net - How can i add controls to a gridview dynamically? -
i have 2 columns, how add contro or 2 each dynamically... each time row created..
i want create columns dynamically.. through iteration.
how achieve that?
place placeholder control in column want add control(s). inside rowdatabound event of gridview add controls placeholder
edit 1
your grid columns in aspx this: -
<columns> <asp:templatefield headertext="column1"> <itemtemplate> <asp:placeholder runat='server' id="column1placeholder"></asp:placeholder> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="column2"> <itemtemplate> <asp:placeholder runat='server' id="column2placeholder"></asp:placeholder> </itemtemplate> </asp:templatefield> </columns> your rowdatabound event this: -
public void gridview_rowdatabound(object sender, gridviewroweventargs e) { if (column1needscontrols) { placeholder placeholder = e.row.findcontrol("column1placeholder") placeholder; textbox textbox1 = new textbox(); placeholder.controls.add(textbox1); } if (column2needscontrols) { placeholder placeholder = e.row.findcontrol("column2placeholder") placeholder; textbox textbox1 = new textbox(); placeholder.controls.add(textbox1); } }
Comments
Post a Comment