Back to Devexpress

How to: Bind an XPCollection to a LookUp

xpo-2000-examples-how-to-bind-an-xpcollection-to-a-lookup.md

latest11.4 KB
Original Source

How to: Bind an XPCollection to a LookUp

  • Oct 06, 2023
  • 7 minutes to read

This example demonstrates how to set up a LookUpEdit in-place editor within a grid control (XtraGrid) for working with a persistent object.

Object Structure

Let’s consider a tracking system for people. Each person has a name and belongs to a specific group (Customer, Client). Information on persons is encapsulated by the Person persistent object. It provides the Name and Group properties which specify a person’s name and the group to which a person belongs. The Group property refers to another persistent object - PersonGroup. This encapsulates a group and contains a single GroupName property.

The Person and PersonGroup persistent objects are implemented as follows:

csharp
public class Person : XPObject {
   public Person() {
      Name = "";
      Group = null;
   }
   public string Name {
       get { return fName; }
       set { SetPropertyValue(nameof(Name), ref fName, value); }
   }
   string fName;

   public PersonGroup Group {
       get { return fGroup; }
       set { SetPropertyValue(nameof(Group), ref fGroup, value); }
   }
   PersonGroup fGroup;

}

public class PersonGroup : XPObject {
   public PersonGroup() {
      GroupName = "";
   }
   public string GroupName {
       get { return fGroupName; }
       set { SetPropertyValue(nameof(GroupName), ref fGroupName, value); }
   }
   string fGroupName;

}
vb
Public Class Person
   Inherits XPObject
   Public Sub New()
      Name = ""
      Group = Nothing
   End Sub
   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 Group() As PersonGroup
       Get
           Return fGroup
       End Get
       Set(ByVal value as PersonGroup)
           SetPropertyValue(NameOf(Group), fGroup, value)
       End Set
   End Property
   Private fGroup As PersonGroup

End Class

Public Class PersonGroup
   Inherits XPObject
   Public Sub New()
      GroupName = ""
   End Sub 'New
   Public Property GroupName() As String
       Get
           Return fGroupName
       End Get
       Set(ByVal value as String)
           SetPropertyValue(NameOf(GroupName), fGroupName, value)
       End Set
   End Property
   Private fGroupName As String

End Class

Add this code to your project and build it before you continue.

In this example, the grid control is used to edit Person objects and these will be represented as records. An in-place LookUpEdit control will be used to edit a person’s group (the Person.Group property) within each record, thus allowing you to set a value by selecting an item from among the existing items. The following image shows the final result of this example (the Person and PersonGroup tables are populated with sample data):

Binding a Collection of Person Objects to the Grid Control

Data-aware controls can work with persistent objects via the XPCollection component. This represents a collection of persistent objects of a specific type. In this example, the grid control should display Person objects, so a new XPCollection must be created and linked to the Person class.

  1. At design time, add the XPCollection component to a form and set it’s XPCollection.ObjectClassInfo property to refer to the Person class (the collection’s name is set to the ‘xpCollectionPerson’ string):

  2. Set the collection’s XPBaseCollection.DisplayableProperties property to the “Name;Group!Key” string. The ‘Group!Key’ item represents the key (a unique value) of the object referred to by the person’s Group property. Persistent objects derived from the XPObject class already have a key property - ‘Oid’. So the ‘Group!Key’ field will return the value of the ‘Oid’ field for a GroupPerson object that is referred to by the person’s Group property. See the Property Descriptors topic for more details on property descriptors supported by XPCollection objects.

  3. Set the grid’s data source to the created xpCollectionPerson object. The grid creates columns for the fields enumerated in the XPBaseCollection.DisplayableProperties property:

The equivalent code is as follows:

csharp
XPCollection xpCollectionPerson = new XPCollection(typeof(Person));
xpCollectionPerson.DisplayableProperties = "Name;Group!Key";
gridControl1.DataSource = xpCollectionPerson;
vb
Dim xpCollectionPerson As New XPCollection(GetType(Person))
xpCollectionPerson.DisplayableProperties = "Name;Group!Key"
GridControl1.DataSource = xpCollectionPerson

Setting Up the In-Place LookUpEdit Editor

The next step is to assign and set up an in-place LookUpEdit control (RepositoryItemLookUpEdit) to edit the values within the grid’s Group column. The LookUpEdit control will provide a list of the groups in its dropdown, so a person’s group can be set by selecting an item from the LookUpEdit’s dropdown.

To populate the LookUpEdit control with a list of groups that are represented by a PersonGroup object, a new XPCollection must be created.

  1. Add a new XPCollection object to a form, call it xpCollectionGroup and set its XPCollection.ObjectClassInfo property to refer to the PersonGroup class (in the same way as with the xpCollectionPerson collection above).

  2. Select the grid’s Group column by clicking its header on the form. Its properties will be displayed in the Properties window. Then invoke a dropdown for the ColumnEdit property and select a LookUpEdit item within it.

  3. Expand the ColumnEdit property in the Properties window, which now displays the settings of the newly created LookUpEdit in-place editor. Set its properties as follows:

These property descriptors have limitations. For additional information, see the Property Descriptors topic.

The equivalent code is as follows:

csharp
XPCollection xpCollectionGroup = new XPCollection(typeof(PersonGroup));
RepositoryItemLookUpEdit lookUpRI = new RepositoryItemLookUpEdit();
lookUpRI.DataSource = xpCollectionGroup;
lookUpRI.DisplayMember = nameof(PersonGroup.GroupName);
lookUpRI.ValueMember = nameof(PersonGroup.Oid);
gridControl1.RepositoryItems.Add(lookUpRI);
// Associate the LookUpEdit editor with the "Group!Key" column.
(gridControl1.MainView as ColumnView).Columns["Group!Key"].ColumnEdit = lookUpRI;
vb
Dim xpCollectionGroup As New XPCollection(GetType(PersonGroup))
Dim lookUpRI As New RepositoryItemLookUpEdit
lookUpRI.DataSource = xpCollectionGroup
lookUpRI.DisplayMember = NameOf(PersonGroup.GroupName)
lookUpRI.ValueMember = NameOf(PersonGroup.Oid)
GridControl1.RepositoryItems.Add(lookUpRI)
' Associate the LookUpEdit editor with the "Group!Key" column.
CType(GridControl1.MainView, ColumnView).Columns("Group!Key").ColumnEdit = lookUpRI

Now, you can run the application and edit the Group column’s values via the LookUpEdit editor:

Full code

The complete code is shown below:

csharp
using DevExpress.Xpo;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Base;

public class Person : XPObject {
   public Person() {
      Name = "";
      Group = null;
   }
   public string Name {
       get { return fName; }
       set { SetPropertyValue(nameof(Name), ref fName, value); }
   }
   string fName;

   public PersonGroup Group {
       get { return fGroup; }
       set { SetPropertyValue(nameof(Group), ref fGroup, value); }
   }
   PersonGroup fGroup;

}

public class PersonGroup : XPObject {
   public PersonGroup() {
      GroupName = "";
   }
   public string GroupName {
       get { return fGroupName; }
       set { SetPropertyValue(nameof(GroupName), ref fGroupName, value); }
   }
   string fGroupName;

}

// ...

XPCollection xpCollectionPerson = new XPCollection(typeof(Person));         
xpCollectionPerson.DisplayableProperties = "Name;Group!Key";
gridControl1.DataSource = xpCollectionPerson;

XPCollection xpCollectionGroup = new XPCollection(typeof(PersonGroup));
RepositoryItemLookUpEdit lookUpRI = new RepositoryItemLookUpEdit();
lookUpRI.DataSource = xpCollectionGroup;
lookUpRI.DisplayMember = nameof(PersonGroup.GroupName);
lookUpRI.ValueMember = nameof(PersonGroup.Oid);
gridControl1.RepositoryItems.Add(lookUpRI);
// Associate the LookUpEdit editor with the "Group!Key" column.
(gridControl1.MainView as ColumnView).Columns["Group!Key"].ColumnEdit = lookUpRI;
vb
Imports DevExpress.Xpo
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraGrid.Views.Base

Public Class Person
   Inherits XPObject
   Public Sub New()
      Name = ""
      Group = Nothing
   End Sub
   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 Group() As PersonGroup
       Get
           Return fGroup
       End Get
       Set(ByVal value as PersonGroup)
           SetPropertyValue(NameOf(Group), fGroup, value)
       End Set
   End Property
   Private fGroup As PersonGroup

End Class

Public Class PersonGroup
   Inherits XPObject
   Public Sub New()
      GroupName = ""
   End Sub 'New
   Public Property GroupName() As String
       Get
           Return fGroupName
       End Get
       Set(ByVal value as String)
           SetPropertyValue(NameOf(GroupName), fGroupName, value)
       End Set
   End Property
   Private fGroupName As String

End Class

' ...

Dim xpCollectionPerson As New XPCollection(GetType(Person))
xpCollectionPerson.DisplayableProperties = "Name;Group!Key"
GridControl1.DataSource = xpCollectionPerson

Dim xpCollectionGroup As New XPCollection(GetType(PersonGroup))
Dim lookUpRI As New RepositoryItemLookUpEdit
lookUpRI.DataSource = xpCollectionGroup
lookUpRI.DisplayMember = NameOf(PersonGroup.GroupName)
lookUpRI.ValueMember = NameOf(PersonGroup.Oid)
GridControl1.RepositoryItems.Add(lookUpRI)
' Associate the LookUpEdit editor with the "Group!Key" column.
CType(GridControl1.MainView, ColumnView).Columns("Group!Key").ColumnEdit = lookUpRI

See Also

How to: Bind an XPCollection to the Grid

Property Descriptors

Create the Edit Order Form with Lookup / Dropdown Control