Ste*_*owe 5 asp.net gridview databound
我在asp.net 2.0中有一个带有行选择链接的数据绑定GridView.当选择一行时,我想以编程方式在所选行下面添加一个表行,以嵌套另一个网格等.
我正在为客户和一篇文章研究这个问题,我认为今晚我的google-fu并不强大.有什么建议?
编辑:我实际上有一个工作的解决方案,但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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27791 次 |
| 最近记录: |