先给大家看一下效果 :
![](https://p-blog.csdn.net/images/p_blog_csdn_net/hb_cattle/demo.bmp)
首先要准备两个数据源,一个是主表数据源,一个是从表数据源。
<
asp:AccessDataSource ID
=
"
AccessDataSource_Master
"
runat
=
"
server
"
DataFile
=
"
~/App_Data/Master-Detail.mdb
"
SelectCommand
=
"
SELECT * FROM [订单]
"
DeleteCommand
=
"
DELETE FROM [订单] WHERE [订单 ID] = ?
"
InsertCommand
=
"
INSERT INTO [订单] ([订单 ID], [订购日期], [采购订单编号], [要求截止日期], [约定截止日期], [货主名称], [送货地址]) VALUES (?, ?, ?, ?, ?, ?, ?)
"
UpdateCommand
=
"
UPDATE [订单] SET [订购日期] = ?, [采购订单编号] = ?, [要求截止日期] = ?, [约定截止日期] = ?, [货主名称] = ?, [送货地址] = ? WHERE [订单 ID] = ?
"
>
</
asp:AccessDataSource
>
<
asp:AccessDataSource ID
=
"
AccessDataSource_Detail
"
runat
=
"
server
"
DataFile
=
"
~/App_Data/Master-Detail.mdb
"
SelectCommand
=
"
SELECT * FROM [订单明细] WHERE ([订单 ID] = ?)
"
DeleteCommand
=
"
DELETE FROM [订单明细] WHERE [订单明细 ID] = ?
"
InsertCommand
=
"
INSERT INTO [订单明细] ([订单明细 ID], [订单 ID], [售出日期], [数量], [单价], [折扣]) VALUES (?, ?, ?, ?, ?, ?)
"
UpdateCommand
=
"
UPDATE [订单明细] SET [订单 ID] = ?, [售出日期] = ?, [数量] = ?, [单价] = ?, [折扣] = ? WHERE [订单明细 ID] = ?
"
>
<
SelectParameters
>
<
asp:SessionParameter Name
=
"
订单_ID
"
SessionField
=
"
OrderID
"
Type
=
"
Int32
"
/>
</
SelectParameters
>
</
asp:AccessDataSource
>
这个地方有一点需要注意的是 SelectParameters 的SessionField 属性。由于 SelectCommand 带一个参数,这个参数的值是存放在Session中的,这个参数的Session名字是OrderID。这个参数主要用来存放主表的当前行的主键值。
到这里所需的数据源已经完成了,下面就要用到这个控件了。
第一步:
将ASPxGridView 控件拖放到页面中,设置数据源为 AccessDataSource_Master,将ShowDetailRow的属性设为True
第二步:
编辑该控件的DetailRow 模板,在其中放置一个ASPxGridView 控件,并设置其数据源为AccessDataSource_Detail
![](https://p-blog.csdn.net/images/p_blog_csdn_net/hb_cattle/demo1.bmp)
第三步:
选中DetailRow模板中的ASPxGridView 控件,为其添加BeforePerformDataSelect 事件,
protected
void
ASPxGridView_Detail_BeforePerformDataSelect(
object
sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
Session["OrderID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
}
以上代码所要完成的功能是,当展开从表时,将主表的当前行的主键值放入Session中,以供从表的数据源使用。
至此,主从表的显示终于完成了.