【问题标题】:How to add additional columns to existing gridview programmatically?如何以编程方式将其他列添加到现有的gridview?
【发布时间】:2011-07-15 16:33:02
【问题描述】:

我在用户控件中有一个网格视图。我正在使用BoundField 在 aspx 页面的 gridview 中显示列。我可以从文件 (.cs) 后面的代码中添加其他列吗?我需要在不同页面中使用的用户控件中添加一些额外的列。

【问题讨论】:

    标签: c# .net asp.net gridview


    【解决方案1】:

    您可以添加网格视图的新cell in RowDataBound 事件,如下所示。 (我在需要的地方添加了 cmets)

    protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.Header)
       {
         TableHeaderCell NewCell = new TableHeaderCell();
         NewCell.Text = "Header Text";
         e.Row.Cells.AddAt(4(Index of Cell where you want to add cell), NewCell);
       }
    
    
    if (e.Row.RowType == DataControlRowType.DataRow)
         {
           TableCell NewCell= new TableCell(); 
           NewCell.ID = "NewCell";
           NewCell.Text = "Text value of cell which you want to display";
           e.Row.Cells.AddAt(4, NewCell);
         }
     }
    

    【讨论】:

      【解决方案2】:

      创建一个在用户控件中添加列的方法,并使其访问器公开。 现在从您拥有该控件对象的 aspx 页面调用该函数。

      【讨论】:

        猜你喜欢
        • 2013-03-09
        • 1970-01-01
        • 1970-01-01
        • 2016-07-24
        • 1970-01-01
        • 2014-04-03
        • 1970-01-01
        • 2018-06-19
        • 2013-09-09
        相关资源
        最近更新 更多