Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DevExpress XtraGrid, bind to property of nested class

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.

like image 416
Suresh Avatar asked Feb 20 '11 07:02

Suresh


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


3 Answers

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

like image 187
Aseem Gautam Avatar answered Oct 10 '22 09:10

Aseem Gautam


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...

like image 20
Tristan Avatar answered Oct 10 '22 08:10

Tristan


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.

like image 26
Benedict Tesha Avatar answered Oct 10 '22 10:10

Benedict Tesha