How to: Implement Master-Detail at Runtime (XPO)

  • 4 min to read

This example shows how to create ASPxGridView controls at runtime to display master-detail data.

 

  1. Define Persistent Classes
  2. Connect to a Database Server
  3. Retrieve Data From the Database
  4. Create Detail ASPxGridView
  5. Create Master ASPxGridView

1. Define Persistent Classes


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;
}
Note

For detailed information on creating XPObjects, see Tutorial 1 - Your First Data-Aware Application with XPO and Tutorial 2 - Relations (One to Many).

2. Connect to a Database Server

To connect XPO to a database server, create an IDataLayer object. The code that creates the data layer must be placed inside the Application_Start event handler in the Global.asax module of your webste. For more information, see Connecting XPO to a Database Server (ASP.NET).


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.XpoDefault.Session = null;
    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);
}

3. Retrieve Data From the Database

To retrieve data from the database, use the XpoDataSource components.

  • Master: Customers

    MasterDetailXPO_Customers

    Set its TypeName property to Customer.

  • Detail: Orders

    MasterDetailXPO_Orders

    Set its TypeName to Order.

    Use the Criteria property to specify the filter condition. Set this property to '[Customer.Oid] = ?'.

    Invoke the Parameter Collection Editor, and add a criteria parameter. This parameter gets its value at runtime from the "Oid" Session field.

    MasterDetailXPO_OrdersParams

  • Handle the page's Init event to bind XpoDataSource components to a database.

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

4. Create Detail ASPxGridView


public class DetailRowTemplate : ITemplate {
    object dataSource = null;
    public DetailRowTemplate(object dataSource) {
        this.dataSource = dataSource;
    }
    public void InstantiateIn(Control container) {
        ASPxGridView detailGridView = new ASPxGridView();
        detailGridView.SettingsDetail.IsDetailGrid = true;
        detailGridView.DataSource = dataSource;
        detailGridView.KeyFieldName = "Oid";
        detailGridView.BeforePerformDataSelect 
        += new EventHandler(detailGridView_BeforePerformDataSelect);
        container.Controls.Add(detailGridView);
    }
    void detailGridView_BeforePerformDataSelect(object sender, EventArgs e) {
        HttpContext.Current.Session["Oid"] = (sender as ASPxGridView).GetMasterRowKeyValue();
    } 
}

5. Create Master ASPxGridView


protected void Page_Load(object sender, EventArgs e) {
    ASPxGridView gv = new ASPxGridView();
    gv.DataSource = dsCustomers;
    gv.SettingsDetail.ShowDetailRow = true;
    gv.SettingsDetail.ShowDetailButtons = true;
    gv.Templates.DetailRow = new DetailRowTemplate(dsOrders);
    gv.KeyFieldName = "Oid";
    ASPxRoundPanel1.Controls.Add(gv);
    gv.DataBind();
}