elementui table 動態列,動態添加TemplateField列

 2023-10-31 阅读 19 评论 0

摘要:GridViewTextTemplate類: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI.WebControls;/// <summary> ///GridViewTextTemplate 的摘要說明 /// </summary> public class GridViewTextTempla

GridViewTextTemplate類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;/// <summary>
///GridViewTextTemplate 的摘要說明
/// </summary>
public class GridViewTextTemplate : System.Web.UI.ITemplate
{private DataControlRowType templateType;private string columnName;private string cId;public GridViewTextTemplate(DataControlRowType type, string colname, string controlId){templateType = type;columnName = colname;cId = controlId;}public void InstantiateIn(System.Web.UI.Control container){// Create the content for the different row types.switch (templateType){case DataControlRowType.Header:// Create the controls and set id properties to put in the headerLiteral myHeadLiteral = new Literal();myHeadLiteral.ID = cId;myHeadLiteral.Text = "<B>" + columnName + "</B>";container.Controls.Add(myHeadLiteral);break;case DataControlRowType.DataRow:// Create the controls and set id properties to put in a data rowTextBox myTextBox = new TextBox();myTextBox.ID = cId;myTextBox.DataBinding += new EventHandler(this.TextBoxDataBinding);myTextBox.Width = 100;container.Controls.Add(myTextBox);break;default:// Insert code to handle unexpected values.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, columnName).ToString();}}
在后臺GridView動態添加TemplateField:
//parameter_description綁定的數據 textParameterDesc是Text的IDTemplateField tField;tField = new TemplateField();tField.HeaderTemplate = new GridViewTextTemplate(DataControlRowType.Header, "說明", "textParameterDescHd");tField.ItemTemplate = new GridViewTextTemplate(DataControlRowType.DataRow, "parameter_description", "textParameterDesc");GridView1.Columns.Add(tField);
或者:
GridView1.Columns.Add(new TemplateField() { HeaderTemplate = new GridViewTextTemplate(DataControlRowType.Header, "序號", ""), ItemTemplate = new GridViewTextTemplate(DataControlRowType.DataRow, "PlanSN", "") });

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/165714.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息