DevExpress的ASPxGridView中实现Master-Detail数据动态绑定
Posted on 2010-05-07 18:07 xujh 阅读(2623) 评论(2) 编辑 收藏 举报ASPxGridView功能强大,在Demo大部分演示Master-Detail开发都是用直接关联SqlDataSource,用Sql语句直接从表里查询出数据来。但考虑到大型系统中经常要把业务逻辑独立处理形成一个业务逻辑层,大部分数据都是调用业务逻辑层中的接口函数取得的。所以Demo中的示例并不适用。
好在.Net中还提供ObjectDataSource,可以代替SqlDataSource里提供数据。而ObjectDataSource可以和业务逻辑层相连。
具体操作如下:
Default.aspx
1 <%@ Page Language="C#" AutoEventWireup="True" CodeBehind="Default.aspx.cs" Inherits="webtst._Default" %>
2
3 <%@ Register Assembly="DevExpress.Web.v9.3, Version=9.3.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
4 Namespace="DevExpress.Web.ASPxPopupControl" TagPrefix="dx" %>
5
6 <%@ Register Assembly="DevExpress.Web.ASPxGridView.v9.3, Version=9.3.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
7 Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dx" %>
8
9 <%@ Register Assembly="DevExpress.Web.ASPxEditors.v9.3, Version=9.3.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
10 Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %>
11
12 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
13
14 <html xmlns="http://www.w3.org/1999/xhtml" >
15 <head runat="server">
16 <title></title>
17 </head>
18 <body>
19
20 <form id="form1" runat="server">
21 <div>
22
23 <dx:ASPxGridView ID="masterGridView" runat="server">
24 <Templates>
25 <DetailRow>
26 <%--detail表格要指定它的DataSourceID为对应的ObjectDataSource --%>
27 <dx:ASPxGridView ID="detailGridView" runat="server" AutoGenerateColumns="False"
28 DataSourceID="CarDataSource"
29 onbeforeperformdataselect="detailGridView_BeforePerformDataSelect">
30 <Columns>
31 <dx:GridViewDataTextColumn FieldName="Id" VisibleIndex="0">
32 </dx:GridViewDataTextColumn>
33 <dx:GridViewDataTextColumn FieldName="CarName" VisibleIndex="1">
34 </dx:GridViewDataTextColumn>
35 </Columns>
36 <%-- detail表格的这个属性要设置为true --%>
37 <SettingsDetail IsDetailGrid= "true" />
38 </dx:ASPxGridView>
39 </DetailRow>
40 </Templates>
41 <%-- master表格的这个属性要设置为true才能显示出detail表 --%>
42 <SettingsDetail ShowDetailRow="true" />
43 </dx:ASPxGridView>
44 </div>
45 <%-- ObjectDataSource的配置属性,指定了SELECT方法为GetMyCar函数, --%>
46 <asp:ObjectDataSource ID="CarDataSource" runat="server"
47 SelectMethod="GetMyCar" TypeName="webtst.Car">
48 <SelectParameters>
49 <%-- 设置了一个Session["ID"]来源地参数id --%>
50 <asp:SessionParameter Name="id" SessionField="ID" Type="Int32" />
51 </SelectParameters>
52 </asp:ObjectDataSource>
53 </form>
54 </body>
55 </html>
56
2
3 <%@ Register Assembly="DevExpress.Web.v9.3, Version=9.3.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
4 Namespace="DevExpress.Web.ASPxPopupControl" TagPrefix="dx" %>
5
6 <%@ Register Assembly="DevExpress.Web.ASPxGridView.v9.3, Version=9.3.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
7 Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dx" %>
8
9 <%@ Register Assembly="DevExpress.Web.ASPxEditors.v9.3, Version=9.3.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
10 Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %>
11
12 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
13
14 <html xmlns="http://www.w3.org/1999/xhtml" >
15 <head runat="server">
16 <title></title>
17 </head>
18 <body>
19
20 <form id="form1" runat="server">
21 <div>
22
23 <dx:ASPxGridView ID="masterGridView" runat="server">
24 <Templates>
25 <DetailRow>
26 <%--detail表格要指定它的DataSourceID为对应的ObjectDataSource --%>
27 <dx:ASPxGridView ID="detailGridView" runat="server" AutoGenerateColumns="False"
28 DataSourceID="CarDataSource"
29 onbeforeperformdataselect="detailGridView_BeforePerformDataSelect">
30 <Columns>
31 <dx:GridViewDataTextColumn FieldName="Id" VisibleIndex="0">
32 </dx:GridViewDataTextColumn>
33 <dx:GridViewDataTextColumn FieldName="CarName" VisibleIndex="1">
34 </dx:GridViewDataTextColumn>
35 </Columns>
36 <%-- detail表格的这个属性要设置为true --%>
37 <SettingsDetail IsDetailGrid= "true" />
38 </dx:ASPxGridView>
39 </DetailRow>
40 </Templates>
41 <%-- master表格的这个属性要设置为true才能显示出detail表 --%>
42 <SettingsDetail ShowDetailRow="true" />
43 </dx:ASPxGridView>
44 </div>
45 <%-- ObjectDataSource的配置属性,指定了SELECT方法为GetMyCar函数, --%>
46 <asp:ObjectDataSource ID="CarDataSource" runat="server"
47 SelectMethod="GetMyCar" TypeName="webtst.Car">
48 <SelectParameters>
49 <%-- 设置了一个Session["ID"]来源地参数id --%>
50 <asp:SessionParameter Name="id" SessionField="ID" Type="Int32" />
51 </SelectParameters>
52 </asp:ObjectDataSource>
53 </form>
54 </body>
55 </html>
56
Default.aspx.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Data;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using DevExpress.Web.ASPxGridView;
8 namespace webtst
9 {
10 public partial class _Default : System.Web.UI.Page
11 {
12 protected void Page_Load(object sender, EventArgs e)
13 {
14
15 masterGridView.DataSource = GetDriver();
16 masterGridView.KeyFieldName = "Id";
17 masterGridView.DataBind();
18 }
19 //后台查询接口,返回主表数据
20 public List<Driver> GetDriver()
21 {
22 List<Driver> drivers = new List<Driver>();
23 drivers.Add(new Driver(0, "Li", "Beijing"));
24 drivers.Add(new Driver(1, "Wang", "Beijing"));
25 drivers.Add(new Driver(2, "Zhang", "Beijing"));
26 drivers.Add(new Driver(3, "Zhao", "Beijing"));
27 drivers.Add(new Driver(4, "Qian", "Beijing"));
28 return drivers;
29 }
30
31 protected void detailGridView_BeforePerformDataSelect(object sender, EventArgs e)
32 {
33 //降主表的主键写入Session,以便于ObjectDataSource在SELECT时调用
34 Session["ID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
35 }
36
37 }
38 //Master类
39 public class Driver
40 {
41 private int _Id;
42 public int Id
43 {
44 get { return _Id; }
45 set { _Id = value; }
46 }
47 private string _Name;
48 public string Name
49 {
50 get { return _Name; }
51 set { _Name = value; }
52 }
53 private string _Addr;
54 public string Addr
55 {
56 get { return _Addr; }
57 set { _Addr = value; }
58 }
59 public Driver(int id, string name, string addr)
60 {
61 Id = id;
62 Name = name;
63 Addr = addr;
64 }
65
66 }
67 //Detail类
68 public class Car
69 {
70 private int _Id;
71 public int Id
72 {
73 get { return _Id; }
74 set { _Id = value; }
75 }
76 private string _CarName;
77 public string CarName
78 {
79 get { return _CarName; }
80 set { _CarName = value; }
81 }
82 public Car(int id, string carname)
83 {
84 Id = id;
85 CarName = carname;
86 }
87 //静态查询函数,对应ObjectDataSource中的SelectMethod,参数对应从主表中传入的Key
88 //在ObjectDataSource根据这个参数自动产生,然后只要指定参数来源于Session中的ID即可
89 public static List<Car> GetMyCar(int id)
90 {
91 //随机产生一个子表的返回数据,此处可以替换成调用其他BLL接口
92 List<Car> cars = new List<Car>();
93
94 cars.Add(new Car(id, "Car" + (id * 10 + 1).ToString()));
95 cars.Add(new Car(id, "Car" + (id * 10 + 2).ToString()));
96 cars.Add(new Car(id, "Car" + (id * 10 + 3).ToString()));
97 cars.Add(new Car(id, "Car" + (id * 10 + 4).ToString()));
98 return cars;
99 }
100
101 }
102 }
103
2 using System.Collections.Generic;
3 using System.Data;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using DevExpress.Web.ASPxGridView;
8 namespace webtst
9 {
10 public partial class _Default : System.Web.UI.Page
11 {
12 protected void Page_Load(object sender, EventArgs e)
13 {
14
15 masterGridView.DataSource = GetDriver();
16 masterGridView.KeyFieldName = "Id";
17 masterGridView.DataBind();
18 }
19 //后台查询接口,返回主表数据
20 public List<Driver> GetDriver()
21 {
22 List<Driver> drivers = new List<Driver>();
23 drivers.Add(new Driver(0, "Li", "Beijing"));
24 drivers.Add(new Driver(1, "Wang", "Beijing"));
25 drivers.Add(new Driver(2, "Zhang", "Beijing"));
26 drivers.Add(new Driver(3, "Zhao", "Beijing"));
27 drivers.Add(new Driver(4, "Qian", "Beijing"));
28 return drivers;
29 }
30
31 protected void detailGridView_BeforePerformDataSelect(object sender, EventArgs e)
32 {
33 //降主表的主键写入Session,以便于ObjectDataSource在SELECT时调用
34 Session["ID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
35 }
36
37 }
38 //Master类
39 public class Driver
40 {
41 private int _Id;
42 public int Id
43 {
44 get { return _Id; }
45 set { _Id = value; }
46 }
47 private string _Name;
48 public string Name
49 {
50 get { return _Name; }
51 set { _Name = value; }
52 }
53 private string _Addr;
54 public string Addr
55 {
56 get { return _Addr; }
57 set { _Addr = value; }
58 }
59 public Driver(int id, string name, string addr)
60 {
61 Id = id;
62 Name = name;
63 Addr = addr;
64 }
65
66 }
67 //Detail类
68 public class Car
69 {
70 private int _Id;
71 public int Id
72 {
73 get { return _Id; }
74 set { _Id = value; }
75 }
76 private string _CarName;
77 public string CarName
78 {
79 get { return _CarName; }
80 set { _CarName = value; }
81 }
82 public Car(int id, string carname)
83 {
84 Id = id;
85 CarName = carname;
86 }
87 //静态查询函数,对应ObjectDataSource中的SelectMethod,参数对应从主表中传入的Key
88 //在ObjectDataSource根据这个参数自动产生,然后只要指定参数来源于Session中的ID即可
89 public static List<Car> GetMyCar(int id)
90 {
91 //随机产生一个子表的返回数据,此处可以替换成调用其他BLL接口
92 List<Car> cars = new List<Car>();
93
94 cars.Add(new Car(id, "Car" + (id * 10 + 1).ToString()));
95 cars.Add(new Car(id, "Car" + (id * 10 + 2).ToString()));
96 cars.Add(new Car(id, "Car" + (id * 10 + 3).ToString()));
97 cars.Add(new Car(id, "Car" + (id * 10 + 4).ToString()));
98 return cars;
99 }
100
101 }
102 }
103