惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
Docker
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 聂微东
MyScale Blog
MyScale Blog
H
Help Net Security
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
M
MIT News - Artificial intelligence
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
P
Proofpoint News Feed
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
Microsoft Security Blog
Microsoft Security Blog
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏

博客园 - 萍踪侠影

c#过滤HTML代码 - 萍踪侠影 - 博客园 也來開發一款日歷控件 制作activeX控件 在aspx頁中獲取DataGrid行序號 - 萍踪侠影 - 博客园 在MasterPage中檢驗User是否登入 - 萍踪侠影 - 博客园 [轉載]Run As Administrator Internet Explorer 7中的层叠样式表兼容性 Microsoft Office Web 组件 PivotTable 基础 ASP讀取excel文件 - 萍踪侠影 - 博客园 一個Ajax.NET的查詢實例 js校验的通用工具 將字串轉換為日期型數據的例子 - 萍踪侠影 - 博客园 將datagrid控件內容輸出到excel文件 - 萍踪侠影 - 博客园 C#常用函数集 IPv6 简介 初探AJAX.NET Visual C# 3.0 新特性概览 遍历XML引擎版本以适应代码 微调您的 Web 站点以适应 Windows XP Service Pack 2
以編程方式為GridView添加欄位 - 萍踪侠影 - 博客园
萍踪侠影 · 2006-12-19 · via 博客园 - 萍踪侠影

方法一:
1。加一個gridview:
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" datasourceid="SqlDataSource1" OnRowCreated="GridView1_RowCreated"><Columns>
<asp:BoundField ReadOnly="True" DataField="ID" InsertVisible="False" SortExpression="ID" HeaderText="ID"></asp:BoundField>
</Columns>
</asp:gridview>

2。兩個事件寫法:

    protected void Button1_Click1(object sender, EventArgs e)
    {
         //添加一個欄位;
        TemplateField tf = new TemplateField();
        tf.HeaderText = "TEST";
        GridView1.Columns.Add(tf);
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        ListItemType lit = (ListItemType)e.Row.RowType;
        if (lit == ListItemType.AlternatingItem || lit == ListItemType.Item)
        {
           //將TextBox加入到新欄位中;
            int c = e.Row.Cells.Count;
            Literal lt = new Literal();
            lt.Text = "<input type=text>";
            TextBox tb = new TextBox();
            tb.ID = "tb";
            tb.EnableViewState = true;
            tb.AutoPostBack = true;
           
            e.Row.Cells[c - 1].Controls.Add(lt);
            e.Row.Cells[c - 1].Controls.Add(tb);
        }
    }

方法二:
實現ITemplate接口的InstantiateIn方法
    public class TextboxTemplate : ITemplate
    {
        /// <summary>
        /// TODO: 自定義TextBox模板列;
        /// </summary>
        private static int Count = 0;
        private bool _isnumber=false;
        private bool _isnull=false;
        private WebControl _wcl;
        private Type _ty;

        public bool ValidateIsNumber
        {
            get
            {
                return _isnumber;
            }
            set
            {
                if (_isnumber != value)
                {
                    _isnumber = value;
                }
            }
        }
        public bool ValidateIsNull
        {
            get
            {
                return _isnull;
            }
            set
            {
                if (_isnull != value)
                {
                    _isnull = value;
                }
            }
        }
        public WebControl Control
        {
            get
            {
                return _wcl;
            }
            set
            {
                _wcl = value;
            }
        }
        public TextboxTemplate(Type tp)
        {
            Count++;
            this._ty = tp;
        }

        public void InstantiateIn(Control container)
        {
            Assembly ass = Assembly.GetAssembly(_ty);
            _wcl = ass.CreateInstance(_ty.ToString()) as WebControl;
            this.Control = _wcl;
            this.Control.ID = "tb" + Count;

            if (this.ValidateIsNull)
            {
                RequiredFieldValidator rf = new RequiredFieldValidator();
                rf.ErrorMessage = "請輸入數據!";
                rf.Text = "*";
                rf.ControlToValidate = this.Control.ID;
                rf.SetFocusOnError = true;
                rf.ValidationGroup = "vg0";
                container.Controls.Add(rf);
            }
            if (this.ValidateIsNumber)
            {
                RegularExpressionValidator re = new RegularExpressionValidator();
                re.ValidationExpression = @"^-?\d+(\.\d{2})?$";
                re.ErrorMessage = "請輸入有效的價格!";
                re.Text = "*";
                re.ControlToValidate = this.Control.ID;
                re.SetFocusOnError = true;
                re.ValidationGroup = "vg0";
                container.Controls.Add(re);
            }
            container.Controls.Add(this.Control);
        }
    }

運用示例:
    protected void Button3_Click(object sender, EventArgs e)
    {
        TemplateColumn tc = new TemplateColumn();
        TextBox tb = new TextBox();
        tc.ItemTemplate = new TextboxTemplate(tb.GetType());
        tc.HeaderText = "單價";
        DataGrid2.Columns.Add(tc);       
    }