Today, some time, the use of CS code to add Edit columns and dynamic add Edit template has been sorted out. Dynamically adding edit columns is simpler, i.e.
TemplateField tf=new TemplateField (), but it is rare to edit column dynamically add edit Template items ItemTemplate. On the Internet that day to find a lot of information, only to find solutions. First, create a class to inherit the template interface ITemplate, and return the template class with the constructor implementation. No more nonsense to say, now give an example, see the clear. The example continues the previous dynamic bound field column.
Create a template class Cs:GVItemTemplate.cs
public class Gridviewitemtemplate:itemplate
{
private string obj; Control object to determine which control is specifically created
Private DataControlRowType Templatetype; Template for current row (Header,item)
private string colname; The character to display for the control, or the field column name of the bound data source
Public Gridviewitemtemplate (DataControlRowType rtype, String controlstr, String coln)
{
Templatetype = Rtype;
ColName = Coln;
obj = controlstr;
}
Public Gridviewitemtemplate (DataControlRowType rtype, String coln)
{
Templatetype = Rtype;
ColName = Coln;
obj = "TextBox";
}
public void InstantiateIn (System.Web.UI.Control container)
{
Switch (templatetype)
{
Case Datacontrolrowtype.header:
Literal HEADLT = new Literal ();
Headlt. Text = colname;
Container. Controls.Add (HEADLT);
Break
Case Datacontrolrowtype.datarow:
Switch (obj. ToLower ())
{
Case "Htmlcheckbox":
HtmlInputCheckBox Cbkeys = new HtmlInputCheckBox ();
Cbkeys.id = "Cb_keys";
Cbkeys. DataBinding + = new EventHandler (this. checkboxdatabinding);
Container. Controls.Add (Cbkeys);
Break
Default
TextBox MyTextBox = new TextBox ();
Mytextbox.databinding + = new EventHandler (this. textboxdatabinding);
Container. Controls.Add (MyTextBox);
Break
}
break;
Default:
break;
}
}
private void textboxdatabinding (Object sender, EventArgs e)
{
TextBox MyTextBox = (textbox) sender;
GridViewRow row = (gridviewrow) Mytextbox.namingcontainer;
Mytextbox.text = System.Web.UI.DataBinder.Eval (row. DataItem, colname). ToString ();
}
private void Checkboxdatabinding (Object sender, EventArgs e)
{
GridViewRow Row=null;
Switch (obj. ToLower ())
{
Case "Htmlcheckbox":
HtmlInputCheckBox Hcbox = (htmlinputcheckbox) sender;
row= (GridViewRow) Hcbox. NamingContainer;
Hcbox. Value = System.Web.UI.DataBinder.Eval (row. DataItem, colname). ToString ();
Break
Default
CheckBox Cbox = (checkbox) sender;
row = (GridViewRow) cbox. NamingContainer;
Cbox. Text = System.Web.UI.DataBinder.Eval (row. DataItem, colname). ToString ();
Break
}
}
}
Page CS code using the GridView control: TestGridView.aspx.cs
void Bindfields (int tbid)
{
TemplateField tf = new TemplateField ();
Tf. ItemTemplate = new Gridviewitemtemplate (Datacontrolrowtype.datarow, "Htmlcheckbox", "DataKey");
Tf. HeaderTemplate = new Gridviewitemtemplate (Datacontrolrowtype.header, "Htmlcheckbox", "");
Tf. Headerstyle.cssclass = "Cbox";
THIS.GV_DATA.COLUMNS.ADD (TF);
}