views:

26

answers:

1

I have a List of complex objects containing other objects within that I give as the data source to a gridview.(currently I'm using BoundFields for the columns). I need to bind data to the columns from the objects within at run time. How can this be done?

A: 

We use LINQ to flatten (denormalise) the entity graph. You can use an anonymous class something like this

        var viewList = (from entity in entityList
                        select new
                        {
                            Field1 = entity.Field1,
                            Field2 = entity.Relation.AnotherField,
                Field3 = entity.Field3 + entity.Relation.YetAnotherField

                        }
                 ).ToList();
        myGridView.DataSource = viewList;
        myGridView.DataBind();

And then use Field1, Field2 on the GridView etc for the data bindings

nonnb
thanks nonnb!. This looks like it'll definitely work
Danaja