代碼如下:
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);
}