asp.net动态添加GridViewRow [英] asp.net dynamically add GridViewRow

查看:25
本文介绍了asp.net动态添加GridViewRow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这篇文章如何以编程方式在 GridView 中插入一行? 但我无法让它添加一行我在 RowDataBound 和 DataBound 事件上尝试过,但它们都不起作用这里是我的代码,如果有人可以告诉我如何动态地在 GridView 的末尾添加一行而不是页脚,这很酷,无论如何这里是我的代码不起作用

I've had a look at this post How to programmatically insert a row in a GridView? but i can't get it to add a row i tried it on RowDataBound and then DataBound event but they both aren't working here is my code if someone could show me how to dynamically add a row to the end of GridView not Footer that would be cool anyway here is my code that doesn't work

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 事件,将任何 Control 添加到 TableCell,并将 TableCell 添加到 GridViewRow.最后将 GridViewRow 添加到指定索引处的 GridView:

Using the RowDataBound event, add any Control to a TableCell, and the TableCell to the GridViewRow. Finally add the GridViewRow to the GridView at a specified index:

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);
} 

这可能不是您真正需要的方式,但它应该会给您一个想法.

This may not be exactly how you need it but it should give you an idea.

这篇关于asp.net动态添加GridViewRow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆