Description:
I'm using XPO. Two persistent objects have a one-to-many association. I want to display a collection of the main objects in one XtraGrid and the associated detail objects of the selected row in the second XtraGrid.
Answer:
Applies to:
XtraGrid, XPO
It is possible to implement this functionality. The first grid can be bound directly to the master collection. The second grid's DataSource must be the same collection as the first grid's. However, it's DataMember property must be set to the name of the detail collection property in the master persistent object.
Let's illustrate this with an example.
There are two persistent classes with a one-to-many association: Customer and Order.
C#public class Customer : XPObject {
...
[Association("CustomerOrders", typeof(Order)), Aggregated]
public XPCollection Orders {
get {
return GetCollection("Orders");
}
}
}
public class Order : XPObject {
...
[Association("CustomerOrders")]
public Customer Customer;
}
If you want to display customers in the first grid and the associated orders in the second grid, please perform the following steps:
- Create the XPCollection component and set its ObjectClassInfo property to Customer.
- Assign the XPCollection to the first (master) grid's DataSource property.
- Assign the XPCollection to the DataSource of the second (detail) grid as well.
- Set the second grid's DataMember property to Orders.
Note The described approach is applicable to a standard DataGrid.
When doing so you may be faced with the problem that the second (detail) XtraGrid automatically creates columns right after assigning the DataSource property. When you later set the DataMember property, the columns are not re-created and therefore looks wrong. All you need to do is to - open the XtraGrid Designer;
- go to the Columns page;
- click the Retrieve Fields button and then customize the new columns.
See Also:
How to Display Master-Detail Tables in Separate Grid Controls
How to display and edit persistent objects with a many-to-many association in the XtraGrid
I used the method you described above with XPCollection but when I tried to replace XPCollection with XPServerCollectionSource, I couldn't get same result. I think they should behave in same manner.
Hi,
To avoid discussing multiple questions in a single thread, I have created a separate ticket on your behalf: How to use two XtraGrid controls to display collections of persistent objects with a one-to-many association in Server Mode. Please refer to it for further correspondence.
Doesn't work with LookUpEdit as master(
Hello,
I've created a separate ticket on your behalf (T467686: How to use XPCollection with LookUpEdit to show master and detail data). It has been placed in our processing queue and will be answered shortly.