Advertisement

10-04 How to: Bind an XPCollection to the DataGrid && 10-05 How to: Bind an XPCollection to the Grid

阅读量:

10-04 How to: Bind an XPCollection to the DataGrid (如何:将 XPCollection 绑定到 DataGrid)

The following example demonstrates how to bind a collection of Customer objects to the DataGrid control at runtime.
以下示例演示如何在时将客户对象的集合绑定到DataGrid控件。

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).
客户类表示单个客户。每个客户可以有多个订单。订单由Order类表示。通过在客户对象中的订单属性(主键)和订单对象中的客户属性(外键)之间创建关系,订单可以与特定客户相关联。

复制代码
    C# 
    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)); } }
    }
    
    
    csharp
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-07-13/jh8dYwotlQfB9q4rp6KUgbxIzZ1R.png)

Binding Collection to DataGrid (将集合绑定到DataGrid)

The DataGrid can be bound to a collection of persistent objects via the DataSource property:
DataGrid可以通过DataSource属性绑定到持久对象的集合:

复制代码
    C#
    DataGrid grid = new DataGrid();
    this.Controls.Add(grid);
    grid.DataSource = new XPCollection<Customer>();
    
    
    csharp
    
    

The image below shows the result.
下图显示了结果。
在这里插入图片描述

10-05 How to: Bind an XPCollection to the Grid (如何:将 XPCollection 绑定到网格)

The following example demonstrates how to bind a collection of Person objects to a grid control at design time and runtime. It’s assumed that a grid control (the XtraGrid) has already been added to a form.
以下示例演示如何在设计时和时将Person对象的集合绑定到网格控件。假设网格控件(XtraGrid)已添加到表单中。

Object Structure (对象结构)

Let’s consider a system for tracking people. Each person has a name and belongs to a specific group (Customer, Client). Information on a person is held by the Person persistent object. It provides the Name and Group properties which specify a person’s name and the group to which they belong. The Group property refers to another persistent object - PersonGroup. This encapsulates a group and contains a single GroupName property.
让我们考虑一个跟踪人的系统。每个人都有一个名字,并且属于一个特定的组( Customer, Client)。一个人的信息由Person持久对象持有。它提供了Name和Group属性,指定一个人的名字和他们所属的组。Group属性引用了另一个持久对象——People Group。这封装了一个组并包含一个GroupName属性。

The Person and PersonGroup persistent objects are implemented as follows:
Person和People Group持久对象的实现如下:

复制代码
    C#
    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;
    
    
    }
    
    
    csharp
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-07-13/kWgRycNPbSABuhLU4zGo5M0ZdanV.png)

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. The following image shows the result of this example (the Person and PersonGroup tables are populated with sample data):
在此示例中,网格控件将用于编辑Person对象,这些对象将表示为记录。下图显示了此示例的结果(Person和PERGroup表填充了示例数据):
在这里插入图片描述

Creating Collection (创建集合)

A grid control 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.
网格控件可以通过XPCollection组件处理持久对象。这表示特定类型的持久对象的集合。在此示例中,网格控件应该显示Person对象,因此必须创建一个新的XPCollection并链接到Person类。

At design time, add the XPCollection component to a form and set it’s XPCollection.ObjectClassInfo property to refer to the Person class:
在设计时,将XPCollection组件添加到表单并设置它的XPCollection. ObjectClassInfo属性以引用Person类:
在这里插入图片描述

As a result, the collection’s XPBaseCollection.DisplayableProperties property is automatically initialized with a list of public properties declared in the Person class. This list specifies which properties will be available at design time for controls that are bound to this collection:
因此,集合的XPBaseCollection.DisplayableProperties属性会使用Person类中声明的公共属性列表自动初始化。此列表指定哪些属性在设计时可用于绑定到此集合的控件:
在这里插入图片描述

Note that additional items are automatically included in the DisplayableProperties property:
请注意,附加项目会自动包含在DisplayableProperties属性中:

This - represents a reference to a persistent object itself (all persistent objects have a public XPBaseObject.This property. The ‘This’ item represents this property.);
表示对持久对象本身的引用(所有持久对象都有一个公共XPBaseObject. This属性。“This”项代表此属性。);

Oid - represents a key field of the Person object (all objects derived from the XPObject have a key field named Oid);
表示Person对象的键字段(从XPObject派生的所有对象都有一个名为Oid的键字段);

Group! - represents a reference to the object that is referred to by the Group property;
表示对Group属性引用的对象的引用;

Group!Key - represents a key field of the object that is referred to by the Group property;
表示Group属性引用的对象的关键字段;

The ‘ This ’, “ Group! ’ and ‘ Group!Key ’ fields are normally used to represent data in a lookup control. See the How to: Bind an XPCollection to a LookUp topic for for an example.
“This”、“ Group!”和“ Group!Key”字段通常用于表示查找控件中的数据。有关示例,请参阅如何:将XPCollection绑定到查找主题。

To create a collection at runtime you can do the following:
要在时创建集合,您可以执行以下操作:

复制代码
    C#
    XPCollection xpCollectionPerson = new XPCollection(typeof(Person));
    
    
    csharp
    
    

Binding Collection to Grid (将集合绑定到网格)

Now the grid control can be bound to the created collection via the DataSource property:
现在可以通过DataSource属性将网格控件绑定到创建的集合:
在这里插入图片描述

After the DataSource property has been initialized the grid automatically creates columns for the fields enumerated in the collection’s XPBaseCollection.DisplayableProperties list. In particular it creates columns for the Oid, Name, Group! and Group!Key fields (the captions for the ‘ Group!’ and ‘ Group!Key ’ fields are automatically set to “Group”).
初始化DataSource属性后,网格会自动为集合的XPBaseCollection.DisplayableProperties列表中枚举的字段创建列。特别是,它会为Oid、Name、Group!和Group!键字段创建列(“ Group!”和“ Group!Key”字段的标题会自动设置为“ Group”)。
在这里插入图片描述

Note
The grid doesn’t automatically create columns for non-browsable fields (see the System.ComponentModel.BrowsableAttribute attribute) and fields that represent collections of objects. In our example, the ‘This’ field is marked as non-browsable, and the ‘Group’ field represents a collection (in XPO any persistent property is always represented as a collection of objects in the binding mechanism). So the grid didn’t create columns for these fields.
网格不会自动为不可浏览的字段(请参阅System. ComponentModel.BrowsableAtual属性)和表示对象集合的字段创建列。在我们的示例中,“This”字段被标记为不可浏览,“Group”字段表示集合(在XPO中,任何持久属性始终表示为绑定机制中的对象集合)。因此网格不会为这些字段创建列。
If any field in the bound data source represents a collection, the XtraGrid will represent this collection in a detail level (as a detail view). Notice the master-detail buttons in the image above.
如果绑定数据源中的任何字段表示一个集合,XtraGrid将在详细级别(作为详细视图)中表示该集合。请注意上图中的主详细按钮。

The following code shows how to bind the grid at runtime (by default, a grid view’s OptionsBehavior.AutoPopulateColumns option is set to true, so the grid automatically creates columns for the available fields in the bound datasource):
以下代码显示了如何在时绑定网格(默认情况下,网格模式的OptionsBeact. AutoPopulateColns选项设置为true,因此网格会自动为绑定数据源中的可用字段创建列):

复制代码
    C#
    gridControl1.DataSource = xpCollectionPerson;
    
    
    csharp
    
    

Accessing Nested Properties (访问嵌套属性)

To access the nested properties of persistent objects, the following syntax can be used for the DisplayableProperties property and in bound controls: ‘PropertyName.NestedPropertyName’.
要访问持久对象的嵌套属性,可以将以下语法用于DisplayableProperties属性和绑定控件中:‘Property tyName. NestedPropertyName’。

Let’s modify the created collection’s XPBaseCollection.DisplayableProperties property so that it returns a person’s name and an associated group’s name. To do this, set this property to the “Name;Group.GroupName” string.
让我们修改已创建集合的XPBaseCollection.DisplayableProperties属性,以便它返回人名和关联组的名称。为此,请将此属性设置为“ Name;Group.GroupName”字符串。
在这里插入图片描述

Repopulate the grid’s column collection. You may need to manually remove columns and reopen the project. As a result the grid only creates columns that correspond to the ‘Name’ and ‘Group.GroupName’ fields. The result is shown below:
重新填充网格的列集合。您可能需要手动删除列并重新打开项目。因此,网格仅创建与“ Name”和“Group. GroupName”字段相对应的列。结果如下所示:
在这里插入图片描述

复制代码
    C#
    xpCollectionPerson.DisplayableProperties = "Name;Group.GroupName";
    gridControl1.MainView.PopulateColumns();
    
    
    csharp
    
    

When you run the application you should see something similar to the following (the database has been populated with a sample data beforehand):
当您应用程序时,您应该会看到类似于以下内容的内容(数据库已预先填充了示例数据):
在这里插入图片描述

The ‘Person.Group’ property is not marked as aggregated (with the AggregatedAttribute attribute). So the nested properties (‘Group.GroupName’) are read-only and cannot be edited. Only the ‘Name’ column can be edited. In XPO, changes that are made in a bound control are automatically posted to a data source on row validation.
Person.Group属性未标记为聚合(使用AggregatedAtoral属性)。因此嵌套属性(Group. GroupName)是只读的,无法编辑。只能编辑名称列。在XPO中,绑定控件中所做的更改在行验证时自动发布到数据源。

If you need to be able to assign a group to a person by choosing a value from a list of the available group names, use the LookUp editor in a column. See the How to: Bind an XPCollection to a LookUp topic for an example.
如果您需要能够通过从可用组名称列表中选择一个值来将组分配给某个人,请在列中使用LookUp编辑器。有关示例,请参阅如何:将XPCollection绑定到LookUp主题。

Full code (完整代码)

The complete code is shown below:
完整的代码如下所示:

复制代码
    C#
    using DevExpress.Xpo;
    
    
    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.GroupName";
    gridControl1.DataSource = xpCollectionPerson;
    
    
    csharp
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-07-13/Fg9BRLJECuKX3Osz7pH6rM4kfv1T.png)

全部评论 (0)

还没有任何评论哟~