5

It's easy to bind an XtraGrid control to a class by setting the FieldName for each column to the name of a property in the underlying class. We have now encountered a situation in which we would like to display data from a class nested in the underlying class.

i.e. we have a "User" class which contains a property called "Address" which is another class called "Address". Within Address are properties like Street, City etc.

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

Please note that Address is not a List, it is a class nested inside of the User class.

We have tried setting the grid column FieldName to "Address.Street" however this doesn't work to pickup the data. I am hoping that this is possible, it seems an elementary feature to not support.

Suresh
  • 53
  • 1
  • 6

4 Answers4

6

Yes you can. Add an unbound column and handle CustomUnboundColumnData Event.

Unbound Columns.
http://documentation.devexpress.com/#WindowsForms/CustomDocument1477

CustomUnboundColumnData
http://documentation.devexpress.com/#WindowsForms/DevExpressXtraGridViewsBaseColumnView_CustomUnboundColumnDatatopic

A G
  • 21,087
  • 11
  • 87
  • 112
  • thank you Aseem. Is that really the best approach for nested class properties. I was really hoping to bind the grid columns directly rather than through the CustomUnboundColumn event. – Suresh Feb 20 '11 at 08:52
  • 1
    Yep. This is the reason UnboundColumn are there for. Definetly better approach than messing around the business objects. – A G Feb 20 '11 at 13:46
  • I have the same kind of problem and checked out the links you provided. do you have any sample code that implements this issue with multi-level nested property (e.g:Customer->Person->Address->HouseNumber) – Naz Aug 12 '11 at 09:26
  • for unbound columns you just need to set 'e.value' for a column/cell. It does not matter how deep your schema is. Just get the data/display text and set 'e.value' – A G Aug 12 '11 at 20:18
3

NestedClass.Property just add like regular properties.

e.g.:

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

    });

best approach remains using the unboundcolumns. But this works...

Tristan
  • 342
  • 2
  • 13
2

Lets assume 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, that you want to bind a column Street to property User.Address.Street, this unfortunately wouldn't work by simply setting FieldName to "Address.Street"

But, if it is important that you accomplish it the way you want, I would suggest that you override 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;
    }
}

Then, set the field name to "Address", instead of "Address.Street" which should do the trick.

Also another approach would be to add another readonly property called UserStreet in the User class:

public class User {

    public string UserName { get; set; }

    public Address UserAddress { get; set; }

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

And then set FieldName to "UserStreet".

Hope this helps.

Benedict Tesha
  • 231
  • 2
  • 3
0

I solve my same bankCode field problem like this;

public class BankAcc : IJPObject
{
    public string uuid { get; set; }
    public int internal_Reference { get; set; }
    public int accountType { get; set; }
    public string accountTypeInfo { get; set; }
    public string code { get; set; }
    public string description { get; set; }
    public string accountNr { get; set; }
    public string iBAN { get; set; }
    public string bankCode { get { return bankRef != null ? bankRef.bank_Code : ""; } set { bankCode = value; }}
    public Bankref bankRef { get; set; } = new Bankref();
}

public class Bankref
{
    public int reference { get; set; }
    public string bank_Code { get; set; }
}