Displaying Nested Objects as Groups

  • 2 minutes to read

For each property of a bound business object, the Data Layout Control generates a layout item with a control. If the bound business object contains a nested object of another class, this nested object can be rendered as a nested layout group, provided that the DataLayoutControl.AllowGeneratingNestedGroups setting is set to True (initially this functionality is disabled). For each field of the nested object, a layout item with a control will be generated and placed inside the created group.

Consider an example in which a Person object contains an AddressInfo object of the Address type.


public class Person {
    public Person() {
        this.AddressInfo = new Address();
    }
    public Person(string fName, string lName, string city, string country, string phone) : this() {
        this.FirstName = fName;
        this.LastName = lName;
        this.AddressInfo.City = city;
        this.AddressInfo.Country = country;
        this.AddressInfo.Phone = phone;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Address AddressInfo { get; set; }
}


public class Address {
    public string City { get; set; }
    public string Country { get; set; }
    public string Phone { get; set; }
}

To bind a Person object to the Data Layout Control, the following code can be used:


BindingSource personBindingSource = new BindingSource();
personBindingSource.DataSource = typeof(Person);
personBindingSource.Add(new Person("Mark", "Shaw", "Las Vegas", "USA", "1 800 634 1234"));
dataLayoutControl1.AllowGeneratingNestedGroups = DevExpress.Utils.DefaultBoolean.True;
dataLayoutControl1.AutoRetrieveFields = true;
dataLayoutControl1.DataSource = personBindingSource;

As a result, the Data Layout Control generates a layout as demonstrated in the image below.

DataLayoutControl-NestedObjects

Note the generated "AddressInfo" group used to edit properties of the nested AddressInfo object.

You can change the name of the generated group using the System.ComponentModel.DataAnnotations.DisplayAttribute with the GroupName parameter. The code below demonstrates how to do this.


using System.ComponentModel.DataAnnotations;

public class Person {
    . . .
    [Display(GroupName="Address Information")]
    public Address AddressInfo { get; set; }
}

DataLayoutControl-NestedProperties-GroupName-DataAnnotAtribute.png

NOTE

To use Data Annotation attributes, ensure that the System.ComponentModel.DataAnnotations assembly is added to your project.

The GroupName parameter also allows you to arrange the nested layout items horizontally (instead of the default vertical alignment) and to render the nested object as a tab. For more information on Data Annotation attributes, see this link.