To dynamically add columns, the key is to implement the ITemplate. InstantiateIn method. The following is an example of adding a GridView template column.
C # code
<%... @ Page Language = "C #" %>
<%... @ Import Namespace = "System. Data" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Script runat = "server">...
ICollection CreateDataSource ()
...{
DataTable dt = new DataTable ();
DataRow dr;
Dt. Columns. Add (new DataColumn ("id", typeof (Int32 )));
Dt. Columns. Add (new DataColumn ("text", typeof (string )));
For (int I = 0; I <6; I ++)
...{
Dr = dt. NewRow ();
Dr [0] = I;
Dr [1] = "list project" + I. ToString ();
Dt. Rows. Add (dr );
}
DataView dv = new DataView (dt );
Return dv;
}
Public class GridViewTemplate: ITemplate
...{
Private DataControlRowType templateType;
Private string columnName;
Public GridViewTemplate (DataControlRowType type, string colname)
...{
TemplateType = type;
ColumnName = colname;
}
Public void InstantiateIn (System. Web. UI. Control container)
...{
Switch (templateType)
...{
Case DataControlRowType. Header:
Literal lc = new Literal ();
Lc. Text = columnName;
Container. Controls. Add (lc );
Break;
Case DataControlRowType. DataRow:
DropDownList drr = new DropDownList ();
Drr. ID = "dropdown ";
Drr. AppendDataBoundItems = true;
Drr. Items. Add (new ListItem ("----- select ------",""));
Drr. Items. Add (new ListItem ("AA", ""));
Drr. Items. Add (new ListItem ("BB", "B "));
Drr. Items. Add (new ListItem ("CC", "c "));
Container. Controls. Add (drr );
Break;
Default:
Break;
}
}
}
Protected void Page_Load (object sender, EventArgs e)
...{
If (! IsPostBack)
...{
TemplateField customField = new TemplateField ();
CustomField. ShowHeader = true;
CustomField. HeaderTemplate = new GridViewTemplate (DataControlRowType. Header, "dynamically add columns ");
CustomField. ItemTemplate = new GridViewTemplate (DataControlRowType. DataRow ,"");
GridView1.Columns. Add (customField );
GridView1.DataSource = CreateDataSource ();
GridView1.DataBind ();
}
}
Protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
...{
If (e. Row. RowType = DataControlRowType. DataRow)
...{
// The values of other fields in the database can be accessed here. You can set the default options. The specific application depends on your use.
// The following is an example.
DataRowView gv = (DataRowView) e. Row. DataItem;
Int itemSeleted = Int32.Parse (gv. Row ["id"]. ToString ()> 3? 0: Int32.Parse (gv. Row ["id"]. ToString ());
DropDownList dr = (DropDownList) e. Row. FindControl ("dropdown ");
Dr. SelectedIndex = itemSeleted;
}
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> example of dynamically adding a template column in The GridView </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Asp: GridView ID = "GridView1" runat = "server" AutoGenerateColumns = "False"
OnRowDataBound = "GridView1_RowDataBound">
<Columns>
<Asp: BoundField HeaderText = "title" DataField = "text"/>
</Columns>
</Asp: GridView>
</Form>
</Body>
</Html>