从对象包含相关领域的子对象将数据绑定到一个GridView [英] Binding data to a GridView from a Object with child objects containing relevant fields

查看:106
本文介绍了从对象包含相关领域的子对象将数据绑定到一个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();

这篇关于从对象包含相关领域的子对象将数据绑定到一个GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆