• GridView动态添加模板列 并动态绑定数据 实现从外部直接传入控件和绑定字段参数


    GridView动态添加模板列 并动态绑定数据 实现从外部直接传入控件和绑定字段参数

    View Code
    public class GridViewTemplate : ITemplate
    {
    #region 参数
    private DataControlRowType templateType; //区分列的类型
    private string headName; //表头名称
    private List<Control> controlList; //控件列表,支持同一列添加多个控件
    private string strSeparator = "^"; //绑定字段与格式之间的分隔符,绑定字段与格式通过传入控件的属性传入。
    private char strSeparatorItem = '/'; //绑定字段之间的分隔符,用于支持多字段绑定。

    public DataControlRowType TemplateType
    {
    get { return templateType; }
    set { templateType = value; }
    }

    public string HeadName
    {
    get { return headName; }
    set { headName = value; }
    }

    public List<Control> ControlList
    {
    get { return controlList; }
    set { controlList = value; }
    }

    public string StrSeparator
    {
    get { return strSeparator; }
    set { strSeparator = value; }
    }

    public char StrSeparatorItem
    {
    get { return strSeparatorItem; }
    set { strSeparatorItem = value; }
    }
    #endregion


    #region 构造
    public GridViewTemplate()
    {
    }
    public GridViewTemplate(DataControlRowType templateType, string headName)
    {
    this.templateType = templateType;
    this.headName = headName;
    }
    public GridViewTemplate(DataControlRowType templateType, List<Control> controlList)
    {
    this.templateType = templateType;
    this.controlList = controlList;
    }
    #endregion


    public void InstantiateIn(Control container)
    {
    switch (templateType)
    {
    case DataControlRowType.Header:
    Literal lc = new Literal();
    lc.Text = headName;
    container.Controls.Add(lc);
    break;
    case DataControlRowType.DataRow:
    foreach (Control control in controlList)
    {
    string strControlType = control.GetType().ToString().Trim();
    switch (strControlType.Substring(strControlType.LastIndexOf('.') + 1, strControlType.Length - strControlType.LastIndexOf('.') - 1).Trim())
    {
    case "Label":
    AddControl(container, control, AddLabel);
    break;
    case "TextBox":
    AddControl(container, control, AddTextBox);
    break;
    case "DropDownList":
    AddControl(container, control, AddDropDownList);
    break;
    case "Button":
    AddControl(container, control, AddButton);
    break;
    default:
    break;
    }
    }
    break;
    default:
    break;
    }
    }


    #region 委托
    /// <summary>
    /// 添加控件--委托
    /// </summary>
    /// <param name="container"></param>
    /// <param name="control"></param>
    private delegate void AddControlDelegate(Control container, Control control);

    /// <summary>
    /// 使用委托添加控件
    /// </summary>
    /// <param name="container"></param>
    /// <param name="control">控件</param>
    /// <param name="addControlDelegate">添加控件方法</param>
    private void AddControl(Control container, Control control, AddControlDelegate addControlDelegate)
    {
    addControlDelegate(container, control);
    }
    #endregion


    #region Label

    private void AddLabel(Control container, Control control)
    {
    Label lblTemp = new Label();
    lblTemp.ID = control.ID;
    lblTemp.Text = ((Label)control).Text;
    lblTemp.ToolTip = ((Label)control).ToolTip;
    lblTemp.Visible = ((Label)control).Visible;
    lblTemp.ApplyStyleSheetSkin(((Label)control).Page);
    lblTemp.ControlStyle.CopyFrom(((Label)control).ControlStyle);
    lblTemp.CopyBaseAttributes((Label)control);
    lblTemp.DataBinding += new EventHandler(Label_DataBinding);
    container.Controls.Add(lblTemp);
    }

    private void Label_DataBinding(object sender, EventArgs e)
    {
    Label lblTemp = (Label)sender;
    GridViewRow row = (GridViewRow)lblTemp.NamingContainer;
    string strTempText = lblTemp.Text.Trim();
    if ((!String.IsNullOrEmpty(strTempText)) && strTempText.Contains(strSeparator))
    {
    lblTemp.Text = "";
    string strBindStr = strTempText.Substring(0, strTempText.LastIndexOf(strSeparator));
    string strBindStrFomat = strTempText.Substring(strTempText.LastIndexOf(strSeparator) + 1, strTempText.Length - strTempText.LastIndexOf(strSeparator) - 1);

    string[] strBindStrSubs = strBindStr.Split(strSeparatorItem);
    string[] strBindStrFomats = strBindStrFomat.Split(strSeparatorItem);
    if (strBindStrSubs.Length == strBindStrFomats.Length)
    {
    for (int i = 0; i < strBindStrSubs.Length; i++)
    {
    lblTemp.Text += DataBinder.Eval(row.DataItem, strBindStrSubs[i], strBindStrFomats[i]).ToString();
    }
    }
    else
    {
    lblTemp.Text = "绑定项的个数与设置绑定项格式的字段个数不同";
    }
    }
    string strTempToolTip = lblTemp.ToolTip.Trim();
    if ((!String.IsNullOrEmpty(strTempToolTip)) && strTempToolTip.Contains(strSeparator))
    {
    lblTemp.ToolTip = "";
    string strBindStr = strTempToolTip.Substring(0, strTempToolTip.LastIndexOf(strSeparator));
    string strBindStrFomat = strTempToolTip.Substring(strTempToolTip.LastIndexOf(strSeparator) + 1, strTempToolTip.Length - strTempToolTip.LastIndexOf(strSeparator) - 1);

    string[] strBindStrSubs = strBindStr.Split(strSeparatorItem);
    string[] strBindStrFomats = strBindStrFomat.Split(strSeparatorItem);
    if (strBindStrSubs.Length == strBindStrFomats.Length)
    {
    for (int i = 0; i < strBindStrSubs.Length; i++)
    {
    lblTemp.ToolTip += DataBinder.Eval(row.DataItem, strBindStrSubs[i], strBindStrFomats[i]).ToString();
    }
    }
    else
    {
    lblTemp.ToolTip = "绑定项的个数与设置绑定项格式的字段个数不同";
    }
    }
    }

    #endregion


    #region TextBox

    private void AddTextBox(Control container, Control control)
    {
    TextBox txtTemp = new TextBox();
    txtTemp.ID = control.ID;
    txtTemp.Text = ((TextBox)control).Text;
    txtTemp.Columns = ((TextBox)control).Columns;
    txtTemp.TextMode = ((TextBox)control).TextMode;
    txtTemp.Rows = ((TextBox)control).Rows;
    txtTemp.ReadOnly = ((TextBox)control).ReadOnly;
    txtTemp.MaxLength = ((TextBox)control).MaxLength;
    txtTemp.Enabled = ((TextBox)control).Enabled;
    txtTemp.Visible = ((TextBox)control).Visible;
    txtTemp.AutoPostBack = ((TextBox)control).AutoPostBack;
    txtTemp.ApplyStyleSheetSkin(((TextBox)control).Page);
    txtTemp.ControlStyle.CopyFrom(((TextBox)control).ControlStyle);
    txtTemp.CopyBaseAttributes((TextBox)control);
    txtTemp.DataBinding += new EventHandler(TextBox_DataBinding);
    container.Controls.Add(txtTemp);
    }

    private void TextBox_DataBinding(object sender, EventArgs e)
    {
    TextBox txtTemp = (TextBox)sender;
    GridViewRow row = (GridViewRow)txtTemp.NamingContainer;
    if ((!String.IsNullOrEmpty(txtTemp.Text.Trim())) && txtTemp.Text.Trim().Contains(strSeparator))
    {
    txtTemp.Text = "";
    string strBindStr = txtTemp.Text.Substring(0, txtTemp.Text.LastIndexOf(strSeparator));
    string strBindStrFomat = txtTemp.Text.Substring(txtTemp.Text.LastIndexOf(strSeparator) + 1, txtTemp.Text.Length - txtTemp.Text.LastIndexOf(strSeparator) - 1);

    string[] strBindStrSubs = strBindStr.Split(strSeparatorItem);
    string[] strBindStrFomats = strBindStrFomat.Split(strSeparatorItem);
    if (strBindStrSubs.Length == strBindStrFomats.Length)
    {
    for (int i = 0; i < strBindStrSubs.Length; i++)
    {
    txtTemp.Text += DataBinder.Eval(row.DataItem, strBindStrSubs[i], strBindStrFomats[i]).ToString();
    }
    }
    else
    {
    txtTemp.Text = "绑定项的个数与设置绑定项格式的字段个数不同";
    }
    }
    }

    #endregion


    #region DropDownList

    private void AddDropDownList(Control container, Control control)
    {
    DropDownList ddlTemp = new DropDownList();
    ddlTemp.ID = control.ID;
    foreach (ListItem item in ((DropDownList)control).Items)
    {
    ddlTemp.Items.Add(item);
    }
    ddlTemp.Visible = ((DropDownList)control).Visible;
    ddlTemp.AutoPostBack = ((DropDownList)control).AutoPostBack;
    ddlTemp.ApplyStyleSheetSkin(((DropDownList)control).Page);
    ddlTemp.ControlStyle.CopyFrom(((DropDownList)control).ControlStyle);
    ddlTemp.CopyBaseAttributes((DropDownList)control);
    container.Controls.Add(ddlTemp);
    }

    #endregion


    #region Button

    private void AddButton(Control container, Control control)
    {
    Button btnTemp = new Button();
    btnTemp.ID = control.ID;
    btnTemp.Text = ((Button)control).Text;
    btnTemp.Enabled = ((Button)control).Enabled;
    btnTemp.ApplyStyleSheetSkin(((Button)control).Page);
    btnTemp.ControlStyle.CopyFrom(((Button)control).ControlStyle);
    btnTemp.CopyBaseAttributes((Button)control);
    btnTemp.DataBinding += new EventHandler(Button_DataBinding);
    container.Controls.Add(btnTemp);
    }

    private void Button_DataBinding(object sender, EventArgs e)
    {
    Button btnTemp = (Button)sender;
    GridViewRow row = (GridViewRow)btnTemp.NamingContainer;
    if ((!String.IsNullOrEmpty(btnTemp.Text.Trim())) && btnTemp.Text.Trim().Contains(strSeparator))
    {
    btnTemp.Text = "";
    string strBindStr = btnTemp.Text.Substring(0, btnTemp.Text.LastIndexOf(strSeparator));
    string strBindStrFomat = btnTemp.Text.Substring(btnTemp.Text.LastIndexOf(strSeparator) + 1, btnTemp.Text.Length - btnTemp.Text.LastIndexOf(strSeparator) - 1);

    string[] strBindStrSubs = strBindStr.Split(strSeparatorItem);
    string[] strBindStrFomats = strBindStrFomat.Split(strSeparatorItem);
    if (strBindStrSubs.Length == strBindStrFomats.Length)
    {
    for (int i = 0; i < strBindStrSubs.Length; i++)
    {
    btnTemp.Text += DataBinder.Eval(row.DataItem, strBindStrSubs[i], strBindStrFomats[i]).ToString();
    }
    }
    else
    {
    btnTemp.Text = "绑定项的个数与设置绑定项格式的字段个数不同";
    }
    }
    }

    #endregion
    }

    前台添加方法:

    protected override void OnInit(EventArgs e)
    {
      TemplateField operationId = new TemplateField();
      operationId .ItemStyle.HorizontalAlign = HorizontalAlign.Right;
      operationId .ItemStyle.Width = 100;
      Label lblId = new Label();
      lblId.ID = "lblId";
      lblId.Text = "Id^{0}";
      List<Control> idControlList = new List<Control>();
      idControlList.Add(lblId);
      operationId.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "合计");
      operationId.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, idControlList );
      gridView.Columns.Add(operationId );

    }

     绑定事件

        protected void GridView_RowCreated(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    for (int k = 0; k < itable; k++)
    {
    TextBox txtTest= (TextBox)e.Row.FindControl("txtTest");
    txtTest.TextChanged += new EventHandler(txtTest_TextChanged);
    }
    }
    }

    protected void txtTest_TextChanged(object sender, EventArgs e)
    {

    }

    GridView动态添加模板列 并动态绑定数据 实现从外部直接传入控件和绑定字段参数

    实现了从外部传入一个控件,利用传入控件的属性值传入绑定字段参数,支持一列添加多个控件,一个控件绑定多个字段。

    里面一个委托方法本来打算从外部传入绑定事件,但是没有实现,所以,有些多余。

    还有就是用到了反射、字符串处理等,效率不高。

    给控件绑定事件需要写在RowCreated里。

    大家多指点,第一次发文章。

  • 相关阅读:
    NX二次开发-创建直线(起点-向量方向-长度)UF_CURVE_create_line
    NX二次开发-创建圆弧(圆心-半径)UF_CURVE_create_arc
    NX二次开发-UFUN获取直线的两个端点UF_CURVE_ask_line_data
    NX-二次开发创建圆弧(三点圆弧)UF_CURVE_create_arc_3point
    NX二次开发-创建直线UF_CURVE_create_line与NXOpen->CreateLine
    NX二次开发-创建圆弧(起点-终点-半径)UF_CURVE_create_arc_point_point_radius
    NX二次开发-UFUN打开信息窗口UF_UI_open_listing_window()
    NX二次开发-UFUN信息窗口打印UF_UI_write_listing_window
    NX二次开发-UFUN单按钮模态对话框窗口打印uc1601用法
    NX二次开发-UFUN打开二进制STL文件函数UF_STD_open_binary_stl_file
  • 原文地址:https://www.cnblogs.com/fei85454645/p/GridView.html
Copyright © 2020-2023  润新知