DevExpress控件使用交流,DevExpress中国社区Dev联系电话 联系电话:023-68661681

ASPxGridView控件常用示例四:显示主从数据

来源:本站原创   发布时间:2012-11-08   浏览:5171次

ASPxGridView网格控件与eXpress Persistent Objects for .NET一同使用可以显示主从数据。在本示例中,你将学习如何创建一个Customers-Orders 数据感知Web应用程序。

1、定义持久化类(Persistent Classes)

C#


using DevExpress.Xpo;
public class Customer : XPObject {
public Customer(Session session) : base(session) { }
string fCustomerName;
public string CustomerName {
get { return fCustomerName; }
set { SetPropertyValue<string>("CustomerName", ref fCustomerName, value); }
}
[Association("Customer-Orders", typeof(Order)), Aggregated]
public XPCollection Orders { get { return GetCollection("Orders"); } }
}
public class Order : XPObject {
public Order(Session session) : base(session) { }
string fProductName;
public string ProductName {
get { return fProductName; }
set { SetPropertyValue<string>("ProductName", ref fProductName, value); }
}
DateTime fOrderDate;
public DateTime OrderDate {
get { return fOrderDate; }
set { SetPropertyValue<DateTime>("OrderDate", ref fOrderDate, value); }
}
[Association("Customer-Orders")]
public Customer Customer;
}


2、连接到数据库服务器

创建一个IDataLayer对象,以实现XPO与数据库服务器链接。创建数据层的代码必须被放入 Global.asax模块的Application_Start 事件处理程序中。

C#

void Application_Start(object sender, EventArgs e) {
string conn = DevExpress.Xpo.DB.AccessConnectionProvider.GetConnectionString(
Server.MapPath("~\\App_Data\\Customer.mdb"));
DevExpress.Xpo.Metadata.XPDictionary dict = new DevExpress.Xpo.Metadata.ReflectionDictionary();
// Initialize the XPO dictionary. 
dict.GetDataStoreSchema(typeof(Customer).Assembly);
DevExpress.Xpo.DB.IDataStore store = DevExpress.Xpo.XpoDefault.GetConnectionProvider(conn,
DevExpress.Xpo.DB.AutoCreateOption.SchemaAlreadyExists);
DevExpress.Xpo.XpoDefault.DataLayer = new DevExpress.Xpo.ThreadSafeDataLayer(dict, store);
DevExpress.Xpo.XpoDefault.Session = nul


3、从数据库中检索数据

使用 XpoDataSource控件可从数据库中检索数据。

Master: Customers

ASPxGridView控件常用示例四:显示主从数据

将TypeName属性设置为Customer。

Detail: Orders

ASPxGridView控件常用示例四:显示主从数据

将 TypeName属性设置为 Order.

使用Criteria属性来指定过滤条件,并将其属性设置为'[Customer.Oid] = ?'。

调用 Parameter Collection Editor,添加标准参数。该参数在运行时从 "Oid" Session字段中获取值。

ASPxGridView控件常用示例四:显示主从数据

通过处理页面的Init事件,可将XpoDataSource控件绑定到数据库。

C#


using DevExpress.Xpo;
Session session;
protected void Page_Init(object sender, EventArgs e) {
session = new Session();
dsCustomers.Session = session;
dsOrders.Session = session;
}


4、创建主从ASPxGridViews

创建两个ASPxGridView控件。将第一个网格控件 (master) 绑定到dsCustomers。将第二个网格(detail)绑定到 dsOrders。在两个网格中选择 Enable editing和Enabledeleting选项。

ASPxGridView控件常用示例四:显示主从数据

5、设置主从关系

点击Edit Templates 任务,调用主(master) ASPxGridView的模板设计器。选择 DetailRow模板,将detail grid网格拖动到Detail Row模板中。

C#


using DevExpress.Web.ASPxGridView;
protected void detailGrid_BeforePerformDataSelect(object sender, EventArgs e) {
Session["Oid"] = (sender as ASPxGridView).GetMasterRowKeyValue();
}


接下来,选择 End Template Editing 任务,启用主网格的 ASPxGridViewDetailSettings.ShowDetailRow选项。

6、运行结果

 ASPxGridView控件常用示例四:显示主从数据

本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/412.html
在线
客服
微信
QQ 电话
023-68661681
返回
顶部