我在用户控件中有一个 GridView 。我正在使用 BoundField
在 aspx 页面的 gridview 中显示列。我可以从代码隐藏文件 (.cs) 添加其他列吗?我需要在不同页面中使用的用户控件中添加一些额外的列。
最佳答案
您可以在 gridview 的 RowDataBound 事件中添加一个新的单元格,如下所示。 (我已经在需要的地方添加了注释)
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);
}
}
关于c# - 如何以编程方式向现有 GridView 添加附加列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6710255/