public partial class WebForm2 : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.GridView gvDynamicArticle; protected void Page_Load(object sender, EventArgs e) { TextBox1 = new TextBox(); } protected void Button1_Click(object sender, EventArgs e) { gvDynamicArticle = new GridView(); TextBox1 = new TextBox(); TextBox1.ID = "TextBox1"; TextBox1.Style["Position"] = "Absolute"; TextBox1.Style["Top"] = "25px"; TextBox1.Style["Left"] = "100px"; SqlConnection con = new SqlConnection("Data Source=ASPSERVER\\SQLEXPRESS;Initial Catalog=Varkeys;Integrated Security=True"); con.Open(); SqlCommand com = new SqlCommand("Select * from CM_MT_REF_CODE", con); SqlDataAdapter ada = new SqlDataAdapter(com); DataSet ds = new DataSet(); ada.Fill(ds); for (int i = 0; i < ds.Tables.Count; i++) { if (ds.Tables[i].Rows.Count > 0) { // GridView gvDynamicArticle = new GridView(); gvDynamicArticle.Width = Unit.Pixel(700); gvDynamicArticle.BorderWidth = Unit.Pixel(5); gvDynamicArticle.Caption = "<div id=\"nifty\" class=\"PostCategory\"> <b class=\"rtop\"><b class=\"r1\"></b><b class=\"r2\"></b><b class=\"r3\"></b><b class=\"r4\"></b></b>" + ds.Tables[i].Rows[0]["CNAME"].ToString() + " Articles<b class=\"rbottom\"><b class=\"r4\"></b><b class=\"r3\"></b><b class=\"r2\"></b><b class=\"r1\"></b></b></div>"; gvDynamicArticle.AutoGenerateColumns = false; gvDynamicArticle.ShowFooter = true; for (int xGH = 0; xGH < ds.Tables[i].Columns.Count; xGH++) { string variable = ds.Tables[i].Columns[xGH].ColumnName.ToString(); if (variable == ds.Tables[i].Columns[xGH].ColumnName.ToString()) { TemplateField tf = null; tf = new TemplateField(); tf.HeaderTemplate = new DynamicGridViewTextTemplate(variable, DataControlRowType.Header); tf.ItemTemplate = new DynamicGridViewTextTemplate(variable, DataControlRowType.DataRow); tf.ControlStyle.BackColor = Color.LightBlue; gvDynamicArticle.Columns.Add(tf); } } gvDynamicArticle.DataSource = ds.Tables[i]; gvDynamicArticle.DataBind(); phDynamicGridHolder.Controls.Add(gvDynamicArticle); } } } protected void Button2_Click(object sender, EventArgs e) { } } } public class DynamicGridViewTextTemplate : ITemplate { string _ColName; DataControlRowType _rowType; public DynamicGridViewTextTemplate(string ColName, DataControlRowType RowType) { _ColName = ColName; _rowType = RowType; } public void InstantiateIn(System.Web.UI.Control container) { switch (_rowType) { case DataControlRowType.Header: Literal lc = new Literal(); lc.Text = "<b>" + _ColName + "</b>"; container.Controls.Add(lc); break; case DataControlRowType.DataRow: TextBox lbl = new TextBox(); lbl.DataBinding += new EventHandler(this.lbl_DataBind); container.Controls.Add(lbl); break; default: break; } } private void lbl_DataBind(Object sender, EventArgs e) { TextBox lbl = (TextBox)sender; GridViewRow row = (GridViewRow)lbl.NamingContainer; lbl.Text = DataBinder.Eval(row.DataItem, _ColName).ToString(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)