[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Collapse]eXpress Persistent Objects
 [Expand]Product Information
 [Expand]Getting Started
 [Expand]Feature Center
 [Expand]Design-Time Features
   How to: Access Data in SQL Query Results
   How to: Bind an XPCollection to a LookUp
   How to: Bind an XPCollection to Standard Controls
   How to: Bind an XPCollection to the DataGrid
   How to: Bind an XPCollection to the Grid
   How to: Build Complex Criteria
   How to: Build Simple Criteria
   How to: Change Inheritance Mapping
   How to: Connect to a SQL Server
   How to: Connect to SQL Azure
   How to: Connect XPO to a Database Other than MS SQL or MS Access
   How to: Control Automatic Saving of Objects when Editing in a Grid
   How to: Create an Aggregated Object
   How to: Delay Property Loading
   How to: Filter By DateTime Values
   How to: Filter Persistent Objects by Type
   How to: Handle Persistent Object Initialization
   How to: Implement a Custom Criteria Language Function Operator
   How to: Implement a Full-Text Search
   How to: Implement Custom Functions and Criteria in LINQ to XPO
   How to: Implement Many-to-Many Relationships
   How to: Implement One-to-One Relationships
   How to: Link Classes Located in Different Assemblies
   How to: Make a Class or Property Non-Persistent
   How to: Make XPCollection Create Objects of Specific Type when They are Created via Bound Controls
   How to: Map to Custom Tables (Views) and Columns
   How to: Obtain a Collection of Persistent Objects by a Set of Their IDs
   How to: Obtain a Collection of Persistent Objects for Processing
   How to: Persist a BLOB Field
   How to: Prevent Changes in a Persistent Object from being Automatically Committed
   How to: Restore Deleted Objects
   How to: Use Pageable Collections
   How to: Use Read-Only Persistent Properties
   How to: Use the ContainsOperator for Objects in a Many-to-Many Relationship
   How to: Validate an Object
   How to: Work with Structures
 [Expand]Member Tables
 [Expand]API Reference
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

How to: Bind an XPCollection to a LookUp

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

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

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

In this example, the grid control will be 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):

Expanded 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 will automatically create columns for the fields enumerated in the XPBaseCollection.DisplayableProperties property:

The equivalent code is as follows:

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

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

For more information on the property descriptors ("!", "!Key", "This", etc) see the Property Descriptors topic.

The equivalent code is as follows:

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

Expanded Full code

The complete code is shown below:

Expanded See Also

Is this topic helpful?​​​​​​​