Skip to main content
.NET Framework 4.5.2+

How to: Bind an XPCollection to a LookUp

  • 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:

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;

}

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):

Ex_BindToLookup_RunGrid

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):

    Ex_BindToLookup_SetObjectClassInfo

  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:

    Ex_BindToLookup_GridPopulateColumns

The equivalent code is as follows:

XPCollection xpCollectionPerson = new XPCollection(typeof(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.

    Ex_BindToLookup_AssignLookupToColumn

  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:

    • DataSource to xpCollectionGroup;
    • DisplayMember to ‘GroupName’;
    • ValueMember to ‘Oid’; ‘Oid’ is a key field inherited by all persistent classes from the XPObject class. It matches the column’s ‘Group!Key’ field.

    Note that there is an alternative way to set up a LookUpEdit editor. A column in which a LookUpEdit control is to be displayed should be bound to the ‘Group!’ field. In this case, the LookUpEdit’s properties must be set as follows:

    • DataSource to xpCollectionGroup;
    • DisplayMember to ‘GroupName’;
    • ValueMember to ‘This’ or null. ‘This’ is a field that returns a reference to an object itself. It matches the ‘Group!’ field which also represents a reference to an object.

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

The equivalent code is as follows:

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;

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

Ex_BindToLookup_RunGrid

Full code

The complete code is shown below:

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;
See Also