我在 asp.net 2.0 中有一个带有行选择链接的数据绑定 GridView。当选择一行时,我想以编程方式在所选行下方添加一个表格行,以便嵌套另一个网格等。
我正在为客户和一篇文章研究这个问题,我认为今晚我的谷歌能力不强。有什么建议么?
编辑:我实际上有一个可行的解决方案,但 Visual Studio 不知何故搞砸了;关闭并重新打开 VS 并重建所有内容解决了问题;-)
我的解决方案发布在下面,如果可能的话请告诉我如何改进。谢谢!
我想我明白了。这是一个似乎有效的解决方案。它可以使用用户控件进行改进,但这就是它的要点:
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)