[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]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Collapse]eXpress Persistent Objects
 [Expand]Product Information
 [Expand]Getting Started
 [Expand]Fundamentals
 [Expand]Feature Center
 [Expand]Concepts
 [Expand]Design-Time Features
 [Collapse]Examples
   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
[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 Standard Controls

This example demonstrates how to build a simple data-aware application using standard controls. The application will be used to view and edit a person's details using standard controls. A person's data will be displayed in a standard DataGridView control in read-only mode. All data editing will be done via the editing section, which contains separate standard controls for each person's property. Changes made in the corresponding editor will be immediately reflected in the DataGridView control. The changes will be persisted in a database by pressing the Save button.

Expanded Persistent Classes Declaration

First, declare the Person and Position classes. In this sample application a person is represented by the Name and Position properties. The Simplified Property Syntax is used to implement property accessors.

Before you continue, add this code to your project and re-build it.

Expanded User Interface Design and Data Binding

The viewing section contains the DataGridView control that is used to navigate through the Person objects. The editing section contains the standard Textbox and Combobox controls that are used to edit the person's Name and Position properties. The Save button is used to save changes to a database.

To display data we use two XPCollection components provided by XPO. The first collection should contain Person objects, and the second collection should contain Position objects.

Follow the steps below to create the required collections:

  • Drag the XPCollection component from the toolbox and drop it onto the main form.
  • Rename the collection to "xpCollectionPersons".
  • Assign the "Person" persistent class to the XPCollection.ObjectClassInfo property:

  • Drag the second XPCollection component from the toolbox and drop it onto the form.
  • Rename this collection to "xpCollectionPositions".
  • Assign the "Position" persistent class to the ObjectClassInfo property.

To bind the controls to the collections, do the following:

  • Open the DataGridView's task pane. Set the DataSource property to "xpCollectionPersons". Uncheck the "Enable Adding", "Enable Editing" and "Enable Deleting" options:

  • Invoke the Columns editor by clicking "Edit Columns..." task.
  • Remove the automatically generated columns except the "Name" column.
  • Create a new "Position" DataGridViewComboBoxColumn column.
  • Set its properties as follows: DataPropertyName to "Position!", DataSource to "xpCollectionPositions", DisplayMember to "PositionName", ValueMember to "This".

  • Bind the TextBox's Text property to the xpCollectionPersons' Name property:

  • Set the ComboBox's DataSource property to "xpCollectionPositions". Bind the SelectedValue property to xpCollectionPersons' Position! property. Set the DisplayMember property to "PositionName" and the ValueMember property to "This".

Expanded Saving the Persistent Objects

To save the changes made, handle the button's Click event as shown below:

Expanded Synchronizing the Viewing and Editing Sections

To update the DataGridView control and immediately reflect the changes made, handle the TextBox's TextChanged and ComboBox's SelectedIndexChanged events:

Expanded Populating the Database

To fill the database with sample data, do the following:

Expanded Results

The following image illustrates the implemented application's main form:

Run the project. It's assumed that the database isn't empty. Select a row in the DataGridView control and edit its data using the corresponding editors displayed within the editing section. Any changes made are immediately reflected within the DataGridView control. To save the changes to the database, press the Save button.

Expanded See Also

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