可编辑的嵌套DataGrid


这张图是从Access 2007中截取过来的,我们可以清晰地看到它的组成类似于嵌套的DataGrid,可收缩可扩展。本文将讲述如何实现嵌套的DataGrid,以及DataGrid的操作。
PS:这里我们将外层DataGrid定义为父DataGrid,内层DataGrid定义为子DataGrid
子DataGrid嵌套的父DataGrid里,是因为子DataGrid作为父DataGrid中某模板列中的控件,特别提醒将子DataGrid放在PlaceHolder中,以便日后可以收缩与扩展。
< asp:DataGrid
DataSource
= ' <%# ((System.Data.DataRowView)Container.DataItem).CreateChildView("ParentTable_ChildTable") %> '
runat
= server
id
= DataGrid2
CellPadding
= " 2 "
BackColor
= " #f1f1f1 "
Font
- Name = " Verdana "
Font
- Size = " 8pt "
AutoGenerateColumns
= false
ItemStyle
- VerticalAlign = " Top "
ShowFooter
= " True "
OnItemCommand
= " DataGrid2_ItemCommand "
OnPageIndexChanged
= " DataGrid2_PageIndexChanged "
OnSortCommand
= " DataGrid2_SortCommand "
AllowPaging
= " True "
AllowSorting
= " True "
EnableViewState
= true >
这里要特别说明CreateChildView事件,下面是MSDN中解释
Returns a DataView  for  the child DataTable with the specified DataRelation.

Namespace: System.Data
Assembly: System.Data (
in  system.data.dll)
在后台代码中需要绑定父DataGrid和子DataGrid,并建立关系
DataTable ldtbParentTable = new  DataTable();
DataTable ldtbChildTable
= new  DataTable();
DataSet ds
= new  DataSet();
DataRelation lobjDataRelation;
……
ds.Tables.Add(ldtbParentTable);
ds.Tables.Add(ldtbChildTable);
lobjDataRelation
= new  DataRelation( " ParentTable_ChildTable " ,ldtbParentTable.Columns[ " CustomerID " ],
                                    ldtbChildTable.Columns[
" ChildCustomerID " ], false );
lobjDataRelation.Nested
= true ;
ds.Relations.Add(lobjDataRelation);
子DataGrid的收缩与扩展只要是控制上面提到的PlaceHolder的可见性。
父DataGrid,子DataGrid的增、删、改与操作一个DataGrid的增、删、改类似,这里就不再累赘。具体可下载代码参考。

代码下载

最后要特别感谢Satya Kanithi,他详细讲述了EditNestedDataGrid,并提供了VB.NET版本。
网址: http://www.codeproject.com/aspnet/EditNestedDataGrid.asp

转载于:https://www.cnblogs.com/MagicBoy/archive/2007/09/14/892621.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值