代码如下:
protected void Page_Load(object sender, EventArgs e)
{
TemplateField mycustomField = new TemplateField(); //创建列实例
mycustomField.ShowHeader = true; // 设置属性
LinkButton lb = new LinkButton();
lb.Text = "Delete";
mycustomField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "删除");
mycustomField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "", lb);
TabelContentListGridView.Columns.Add(mycustomField); //添加列到TabelContentListGridView控件
// 这种绑定翻页会丢失数据
// 下面的绑定数据可以保持 不过也要清零列不然后出现很多重复列
// TabelContentListGV.Columns.Clear(); //清零列
// System.Web.UI.WebControls.BoundField customField = new BoundField();
// customField.DataField = dt.Columns[i].ColumnName;
// customField.HeaderText = dt.Columns[i].ColumnName;
// customField.ItemStyle.CssClass = dt.Columns[i].ColumnName;
// TabelContentListGridView.Columns.Add(customField);
}