KB Article A2750
Visible to All Users

How to use two XtraGrid controls to display collections of persistent objects with a one-to-many association

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:

  1. Create the XPCollection component and set its ObjectClassInfo property to Customer.
  2. Assign the XPCollection to the first (master) grid's DataSource property.
  3. Assign the XPCollection to the DataSource of the second (detail) grid as well.
  4. 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
  5. open the XtraGrid Designer;
  6. go to the Columns page;
  7. 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
Show previous comments (1)
DevExpress Support Team 11 years ago

    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.

    IK IK
    Igor Kostashchuk 8 years ago

      Doesn't work with LookUpEdit as master(

      DevExpress Support Team 8 years ago

        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.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.