在AspxGridView中动态添加模板列

grid为某一AspxGridView

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        for (int i = 0; i < 3;i++ )
        {
            GridViewDataTextColumn gvc = new GridViewDataTextColumn();
            gvc.Caption = "asd";
            gvc.DataItemTemplate = new ItemTemplate(i);
            grid.Columns.Add(gvc);
        }       
    }
    public class ItemTemplate:ITemplate
    {
        int i;
        public ItemTemplate(int i)
        {
            this.i = i;
        }
        public void InstantiateIn(Control container)
        {
            //ASPxSpinEdit tb = new ASPxSpinEdit();
            ////tb.Text = "lb";
            //tb.ID = "tb"+i.ToString();
            ////tb.Text = DataBinder.Eval((container as GridViewDataItemTemplateContainer).DataItem, "GroupName ").ToString();
            //tb.SpinButtons.ShowIncrementButtons = false;
            //tb.Width = Unit.Pixel(50);
            //tb.NumberType = SpinEditNumberType.Float;
            //tb.AllowNull = true;
            ////tb.MinValue = 0;
            //tb.MaxValue = 100;
            //tb.Increment = 1;
            //tb.LargeIncrement = 1;
            ASPxTextBox tb = new ASPxTextBox();
            tb.ID = "tb" + i.ToString();
            container.Controls.Add(tb);
            //tb.ValidationSettings.RegularExpression.ValidationExpression=@"(\d+\.\d*)|(\d*\.\d+)|(\d+)";
            //tb.ValidationSettings.RegularExpression.ErrorText = "只能输入数字!";
            //tb.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.Text;
            //tb.ValidationSettings.ErrorTextPosition = DevExpress.Web.ASPxClasses.ErrorTextPosition.Bottom;
            RegularExpressionValidator rv = new RegularExpressionValidator();
            rv.ValidationExpression = @"(\d+\.\d*)|(\d*\.\d+)|(\d+)";
            rv.Display = ValidatorDisplay.Dynamic;
            rv.Text = "只能输入数字!";           
            rv.SetFocusOnError = false;
            rv.EnableClientScript = true;
            rv.ControlToValidate = tb.ID;
            container.Controls.Add(rv);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {       
        for(int i=0;i<3;i++)
        {
            ASPxTextBox tb = grid.FindRowCellTemplateControl(i, null, "tb0") as ASPxTextBox;
            Response.Write(Convert.ToDouble(tb.Text).ToString() + "<br>");
        }
    }
}

你可能感兴趣的:(GridView)