Asp.net| News | templates
The key to dynamically adding columns 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 item" + 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 ("-----Please select------", ""));
Drr. Items.Add (New ListItem ("AA", "a"));
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 adding 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)
... {
Here you can access the value of the other fields of the database, you can set the default selection, specific application, to see their own play.
Here's an example, extrapolate, no longer crap.
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>
<title>gridview examples of dynamically adding template columns </title>
<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>