原創(chuàng)|其它|編輯:郝浩|2012-11-08 14:29:23.000|閱讀 1311 次
概述:ASPxGridView網(wǎng)格控件與eXpress Persistent Objects for .NET一同使用可以顯示主從數(shù)據(jù)。在本示例中,你將學(xué)習(xí)如何創(chuàng)建一個Customers-Orders 數(shù)據(jù)感知Web應(yīng)用程序。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
ASPxGridView網(wǎng)格控件與eXpress Persistent Objects for .NET一同使用可以顯示主從數(shù)據(jù)。在本示例中,你將學(xué)習(xí)如何創(chuàng)建一個Customers-Orders 數(shù)據(jù)感知Web應(yīng)用程序。
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、連接到數(shù)據(jù)庫服務(wù)器
創(chuàng)建一個IDataLayer對象,以實(shí)現(xiàn)XPO與數(shù)據(jù)庫服務(wù)器鏈接。創(chuàng)建數(shù)據(jù)層的代碼必須被放入 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、從數(shù)據(jù)庫中檢索數(shù)據(jù)
使用 XpoDataSource控件可從數(shù)據(jù)庫中檢索數(shù)據(jù)。
Master: Customers

將TypeName屬性設(shè)置為Customer。
Detail: Orders

將 TypeName屬性設(shè)置為 Order.
使用Criteria屬性來指定過濾條件,并將其屬性設(shè)置為'[Customer.Oid] = ?'。
調(diào)用 Parameter Collection Editor,添加標(biāo)準(zhǔn)參數(shù)。該參數(shù)在運(yùn)行時從 "Oid" Session字段中獲取值。

通過處理頁面的Init事件,可將XpoDataSource控件綁定到數(shù)據(jù)庫。
C#
using DevExpress.Xpo;
Session session;
protected void Page_Init(object sender, EventArgs e) {
session = new Session();
dsCustomers.Session = session;
dsOrders.Session = session;
}
4、創(chuàng)建主從ASPxGridViews
創(chuàng)建兩個ASPxGridView控件。將第一個網(wǎng)格控件 (master) 綁定到dsCustomers。將第二個網(wǎng)格(detail)綁定到 dsOrders。在兩個網(wǎng)格中選擇 Enable editing和Enabledeleting選項(xiàng)。

5、設(shè)置主從關(guān)系
點(diǎn)擊Edit Templates 任務(wù),調(diào)用主(master) ASPxGridView的模板設(shè)計(jì)器。選擇 DetailRow模板,將detail grid網(wǎng)格拖動到Detail Row模板中。
C#
using DevExpress.Web.ASPxGridView;
protected void detailGrid_BeforePerformDataSelect(object sender, EventArgs e) {
Session["Oid"] = (sender as ASPxGridView).GetMasterRowKeyValue();
}
接下來,選擇 End Template Editing 任務(wù),啟用主網(wǎng)格的 ASPxGridViewDetailSettings.ShowDetailRow選項(xiàng)。
6、允許結(jié)果

本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自:DevExpress中文網(wǎng)