gpt4 book ai didi

c# - asp.net动态添加GridViewRow

转载 作者:太空狗 更新时间:2023-10-29 17:49:49 25 4
gpt4 key购买 nike

我看过这篇文章How to programmatically insert a row in a GridView?但我无法让它添加一行我在 RowDataBound 上尝试过,然后在 DataBound 事件上尝试过,但它们都不起作用这里是我的代码,如果有人可以告诉我如何动态地添加一行到 GridView 的末尾而不是 Footer 那反正我的代码不起作用

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
}

最佳答案

使用 RowDataBound 事件,将任何控件添加到 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);
}

这可能不是您所需要的,但它应该给您一个想法。

关于c# - asp.net动态添加GridViewRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8700432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com