2
我在用户控件中有一个gridview。我正在使用BoundField
在aspx页面中显示gridview中的列。我可以从代码后面的文件(.cs)添加更多列吗?我需要在不同的页面中使用用户控件添加一些额外的列。如何以编程方式将其他列添加到现有的gridview?
我在用户控件中有一个gridview。我正在使用BoundField
在aspx页面中显示gridview中的列。我可以从代码后面的文件(.cs)添加更多列吗?我需要在不同的页面中使用用户控件添加一些额外的列。如何以编程方式将其他列添加到现有的gridview?
创建一种在用户控件中添加列的方法,并使其公开。 现在从aspx页面调用那个你有那个控制对象的函数。
您可以添加gridview的新事件cell in 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);
}
}