以編程方式為GridView添加欄位

方法一:
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);       
    }

posted @ 2006-12-19 21:34  萍踪侠影  阅读(745)  评论(2编辑  收藏  举报