public class ColumnTemplate : ITemplate
{
string columnName = "/";
public ColumnTemplate(string colname)
{
columnName = colname;
}
public void InstantiateIn(Control container) //关键是这个方法
{
LiteralControl l = new LiteralControl();
l.ID = "topicname";
l.Text = columnName;
l.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(l);
}
//
public void OnDataBinding(object sender, EventArgs e)
{
LiteralControl l = (LiteralControl)sender;
GridViewRow container = (GridViewRow)l.NamingContainer; //update TAG
l.Text = ((DataRowView)container.DataItem)[columnName].ToString();
}
}
TemplateField tf=new TemplateField();
tf.HeaderText = "删除";
tf.ItemTemplate = new ColumnTemplate("ID"); //输入需要绑定的字段名
gridview1.Columns.Add(tf);
{
string columnName = "/";
public ColumnTemplate(string colname)
{
columnName = colname;
}
public void InstantiateIn(Control container) //关键是这个方法
{
LiteralControl l = new LiteralControl();
l.ID = "topicname";
l.Text = columnName;
l.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(l);
}
//
public void OnDataBinding(object sender, EventArgs e)
{
LiteralControl l = (LiteralControl)sender;
GridViewRow container = (GridViewRow)l.NamingContainer; //update TAG
l.Text = ((DataRowView)container.DataItem)[columnName].ToString();
}
}
TemplateField tf=new TemplateField();
tf.HeaderText = "删除";
tf.ItemTemplate = new ColumnTemplate("ID"); //输入需要绑定的字段名
gridview1.Columns.Add(tf);