C # DevExpress XtraGrid, binding to a property of a nested class

It is easy to associate an XtraGrid control with a class by setting the FieldName for each column to the property name in the base class. We are faced with a situation in which we would like to display data from a class nested in a base class.

i.e. we have a class "User" that contains the property "Address", which is another class "Address". Inside the address are objects such as street, city, etc.

We would like to display UserName (from the User class) and Street (from the Address class) on the grid. Is it possible?

Note that Address is not a List; it is a class nested inside the User class.

We tried to set the FieldName grid column to "Address.Street", however this does not work for data collection. I hope this is possible, it seems the elementary function does not support.

+5
source share
3 answers

Suppose you have the following classes in your code.

1) Address class

public class Address {
    public string Street { get; set; }

    public string City { get; set; }
}

2) User class

public class User {

    public string UserName { get; set; }

    public Address UserAddress { get; set; }
}

Now, when you want to bind the Street column to the User.Address.Street property, this, unfortunately, will not work, just setting the FieldName to "Address.Street"

But if it is important that you execute it the way you want, I would suggest that you redefine the ToString () method of the Address class as follows:

public class Address {
    public string Street { get; set; }

    public string City { get; set; }

    //Override ToString() method
    public override string ToString() {
        return this.Street;
    }
}

"", ".", .

- readonly, UserStreet User:

public class User {

    public string UserName { get; set; }

    public Address UserAddress { get; set; }

    public UserStreet {
        get { return UserAddress != null ? UserAddress.Street : ""; } 
    }
}

FieldName "UserStreet".

, .

+2

NestedClass.Property .

:.

       settings.Columns.Add(column =>
    {
        column.Caption = "NestedClass";
        column.FieldName = "NestedClass.DataEntry";
        column.Name = "NestedClass";

    });

- unboundcolumns. ...

+3

All Articles