从对象包含相关领域的子对象将数据绑定到一个GridView
我有一个包含在我给作为数据源到GridView。(我目前使用绑定列的列)其他对象的复杂对象的名单。我需要在运行时间内将数据绑定到从对象的列。如何才能做到这一点?
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?
使用LINQ的投影压平(denormalise)的实体图。您可以创建一个新的视图模型
键入类,或者绑定到一个匿名类,像这样:
Use a LINQ projection to flatten (denormalise) the entity graph. You can either create a new ViewModel
type class, or alternatively bind to 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();
使用字段1
,字段2
在 GridView控件
属性对于数据绑定。
Use Field1
, Field2
on the GridView
properties for the data bindings.
修改
以上预测,在lambda语法:
The above projection, in Lambda syntax:
var viewList = entityList
.Select(entity => new
{
Field1 = entity.Field1,
Field2 = entity.Relation.AnotherField,
Field3 = entity.Field3 + entity.Relation.YetAnotherField
})
.ToList();