我看过这篇文章如何以编程方式在GridView中插入一行?但我不能让它添加一行我在RowDataBound上尝试它然后DataBound事件,但他们都不工作在这里是我的代码,如果有人可以告诉我如何动态添加行到GridView而不是页脚结束无论如何这将是很酷的是我的代码不起作用
protected void CustomGridView_DataBound(object sender, EventArgs e)
{
int count = ((GridView)sender).Rows.Count;
GridViewRow row = new GridViewRow(count+1, -1, DataControlRowType.DataRow, DataControlRowState.Insert);
//lblCount.Text = count.ToString();
// count is correct
// row.Cells[0].Controls.Add(new Button { Text="Insert" });
// Error Here adding Button
Table table = (Table)((GridView)sender).Rows[0].Parent;
table.Rows.Add(row);
// table doesn't add row
}
Run Code Online (Sandbox Code Playgroud)
使用RowDataBound事件,将任何Control添加到TableCell,将TableCell添加到GridViewRow.最后在指定的索引处将GridViewRow添加到GridView:
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = new GridViewRow(e.Row.RowIndex+1, -1, DataControlRowType.DataRow, DataControlRowState.Insert);
TableCell cell = new TableCell();
cell.ColumnSpan = some_span;
cell.HorizontalAlign = HorizontalAlign.Left;
Control c = new Control(); // some control
cell.Controls.Add(c);
row.Cells.Add(cell);
((GridView)sender).Controls[0].Controls.AddAt(some_index, row);
}
Run Code Online (Sandbox Code Playgroud)
这可能不是你需要的,但它应该给你一个想法.