Back to Devexpress

How to: Bind an XPCollection to the DataGrid

xpo-2996-examples-how-to-bind-an-xpcollection-to-the-datagrid.md

latest4.1 KB
Original Source

How to: Bind an XPCollection to the DataGrid

  • Sep 15, 2020
  • 3 minutes to read

The following example demonstrates how to bind a collection of Customer objects to the DataGrid control at runtime.

Object Structure

The Customer class represents a single customer. Each customer can have multiple orders. Orders are represented by the Order class. Orders can be associated with a specific customer by creating a relationship between the Orders property in the Customer object (the primary key) and the Customer property in the Orders object (the foreign key).

csharp
public class Order : XPObject {
    public string ProductName {
        get { return fProductName; }
        set { SetPropertyValue(nameof(ProductName), ref fProductName, value); }
    }
    string fProductName;

    public DateTime OrderDate {
        get { return fOrderDate; }
        set { SetPropertyValue(nameof(OrderDate), ref fOrderDate, value); }
    }
    DateTime fOrderDate;

    [Association("Customer-Orders")]
    public Customer Customer {
        get { return fCustomer; }
        set { SetPropertyValue(nameof(Customer), ref fCustomer, value); }
    }
    Customer fCustomer;

}

public class Customer : XPObject {
    public string Name {
        get { return fName; }
        set { SetPropertyValue(nameof(Name), ref fName, value); }
    }
    string fName;

    public int Age {
        get { return fAge; }
        set { SetPropertyValue(nameof(Age), ref fAge, value); }
    }
    int fAge;

    // When one object relates to other objects, 
    // the "many" end of the association must be defined by an XPCollection class.
    [Association("Customer-Orders")]
    public XPCollection<Order> Orders { get { return GetCollection<Order>(nameof(Orders)); } }
}
vb
Public Class Order
    Inherits XPObject
    Public Property ProductName() As String
     Get
         Return fProductName
     End Get
     Set(ByVal value as String)
         SetPropertyValue(NameOf(ProductName), fProductName, value)
     End Set
 End Property
 Private fProductName As String

    Public Property OrderDate() As DateTime
     Get
         Return fOrderDate
     End Get
     Set(ByVal value as DateTime)
         SetPropertyValue(NameOf(OrderDate), fOrderDate, value)
     End Set
 End Property
 Private fOrderDate As DateTime

    <Association("Customer-Orders")> _
    Public Property Customer() As Customer
     Get
         Return fCustomer
     End Get
     Set(ByVal value as Customer)
         SetPropertyValue(NameOf(Customer), fCustomer, value)
     End Set
 End Property
 Private fCustomer As Customer

End Class

Public Class Customer
    Inherits XPObject
    Public Property Name() As String
     Get
         Return fName
     End Get
     Set(ByVal value as String)
         SetPropertyValue(NameOf(Name), fName, value)
     End Set
 End Property
 Private fName As String

    Public Property Age() As Integer
     Get
         Return fAge
     End Get
     Set(ByVal value as Integer)
         SetPropertyValue(NameOf(Age), fAge, value)
     End Set
 End Property
 Private fAge As Integer

    ' When one object relates to other objects, 
                ' the "many" end of the association must be defined by an XPCollection class.
    <Association("Customer-Orders")> _
    Public ReadOnly Property Orders() As XPCollection(Of Order)
        Get
            Return GetCollection(Of Order)(NameOf(Orders))
        End Get
    End Property
End Class

Binding Collection to DataGrid

The DataGrid can be bound to a collection of persistent objects via the DataSource property:

csharp
DataGrid grid = new DataGrid();
this.Controls.Add(grid);
grid.DataSource = new XPCollection<Customer>();
vb
Dim grid As DataGrid = New DataGrid()
Me.Controls.Add(grid)
grid.DataSource = New XPCollection(Of Customer)()

The image below shows the result.

See Also

XPO Templates