如何以编程方式插入一个GridView行? [英] How to programmatically insert a row in a GridView?

查看:142
本文介绍了如何以编程方式插入一个GridView行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个行选择链接在asp.net 2.0数据绑定的GridView。当选择了行,我希望以编程添加一个表行中的所选行的下方,以巢另一个网格等人

i have a databound GridView in asp.net 2.0 with a row-selection link. When a row is selected, I want to programmatically add a table row below the selected row, in order to nest another grid et al.

我研究这个为客户和一篇​​文章,我认为我的谷歌福是不是今晚强。有什么建议?

I am researching this for a client and for an article, and i think my google-fu is not strong tonight. Any suggestions?

编辑:其实我有一个工作的解决方案,但Visual Studio中莫名其妙地被螺帽固定起来;关闭并重新打开VS和重建一切都解决了问题; - )

I actually had a working solution but Visual Studio was nutted up somehow; closing and re-opening VS and rebuilding everything fixed the problem ;-)

我的解决办法是贴在下面,请告诉我如何使它更好,如果可能的。谢谢!

My solution is posted below, please tell me how to make it better if possible. Thanks!

推荐答案

我想我想通了。这里是一个似乎工作的解决方案。它可以通过用户控制来改善,但是这是它的要点:

I think I figured it out. Here is a solution that seems to work. It could be improved using user controls but this is the gist of it:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && 
        (e.Row.RowState & DataControlRowState.Selected) > 0)
    {
        Table tbl = (Table)e.Row.Parent;
        GridViewRow tr = new GridViewRow(e.Row.RowIndex + 1, -1,
            DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
        TableCell tc = new TableCell();
        tc.ColumnSpan = GridView1.Columns.Count;
        tc.Controls.Add(
            makeChildGrid(Convert.ToInt32(
                ((DataRowView)e.Row.DataItem)["ROW_ID_FIELD"])));
        tr.Cells.Add(tc);
        tbl.Rows.Add(tr);
    }
}

protected GridView makeChildGrid(int id)
{
    GridView gv = new GridView();
    SqlDataSource sqlds = new SqlDataSource();
    sqlds.DataSourceMode = SqlDataSourceMode.DataSet;
    sqlds.ConnectionString = SqlDataSource1.ConnectionString;
    sqlds.SelectCommand = "SELECT * from MY_TABLE_NAME " +
        "WHERE KEY_FIELD = " + id.ToString();
    DataView dv = (DataView)sqlds.Select(DataSourceSelectArguments.Empty);
    gv.DataSource = dv;
    gv.DataBind();    //not sure this is necessary...?
    return gv;
}

这篇关于如何以编程方式插入一个GridView行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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