Please find the sample code for dynamically adding controls to gridview.
The datatable is also created dynamically for code clarity. And a Two column is added , once is TextBox and other is a Dropdownlist column. The same can be applied for other controls also.
If any events should be added to the control , then the "Attribute" property of the control will be used like:
Control1.Attributes.Add("","");
protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("Name"); dt.Columns.Add("City"); DataRow dr; for (int i = 0; i < 5; i++) { dr = dt.NewRow(); dr[0] = "Name " + (i + 1); dr[1] = "City " + (i + 1); dt.Rows.Add(dr); } grd.DataSource = dt; grd.DataBind(); }
protected void OnDataBound(object sender, GridViewRowEventArgs e) { string STR = e.Row.Cells[0].Text;
TableCell TC = new TableCell(); e.Row.Cells.Add(TC); TC = new TableCell(); e.Row.Cells.Add(TC); if (e.Row.RowType == DataControlRowType.Header) { e.Row.Cells[2].Text = "Text Box"; e.Row.Cells[3].Text = "DropDownList"; } else { TextBox txt = new TextBox(); txt.ID = "TEST"; e.Row.Cells[2].Controls.Add(txt); DropDownList ddl = new DropDownList(); ddl.ID = "ddl"; e.Row.Cells[3].Controls.Add(ddl); } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|