【发布时间】:2011-07-15 16:33:02
【问题描述】:
我在用户控件中有一个网格视图。我正在使用BoundField 在 aspx 页面的 gridview 中显示列。我可以从文件 (.cs) 后面的代码中添加其他列吗?我需要在不同页面中使用的用户控件中添加一些额外的列。
【问题讨论】:
我在用户控件中有一个网格视图。我正在使用BoundField 在 aspx 页面的 gridview 中显示列。我可以从文件 (.cs) 后面的代码中添加其他列吗?我需要在不同页面中使用的用户控件中添加一些额外的列。
【问题讨论】:
您可以添加网格视图的新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);
}
}
【讨论】:
创建一个在用户控件中添加列的方法,并使其访问器公开。 现在从您拥有该控件对象的 aspx 页面调用该函数。
【讨论】: