GridView控件绑定嵌套类的属性 [英] GridView bound with Properties of nested class

查看:115
本文介绍了GridView控件绑定嵌套类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的东西下面列出的对象映射。当我尝试NestedClass的属性绑定在GridView我得到的错误:

I have an object map similar to what's listed below. When I try to bind the properties of NestedClass in a GridView I get the error:

名为'NestedClass.Name'字段或属性没有被选定的数据源上找到的。

"A field or property with the name 'NestedClass.Name' was not found on the selected data source."

GridView控件绑定到一个ObjectDataSource和ObjectDataSource控件绑定到BoundClass的完全填充的实例。

The GridView is bound to an ObjectDataSource and the ObjectDataSource is bound to a fully populated instance of BoundClass.

有没有解决这个办法吗?

Is there any way around this?

示例类:

public class BoundClass
{
    public string Name { get; set; }
    public NestedClass NestedClass { get; set; }
}

public class NestedClass
{
    public string Name { get; set; }
}


推荐答案

的实例中,只有直接属性可以显示在一个BoundField柱

Only immediate properties of an instance can be displayed in a BoundField column.

我们需要使用的DataBinder.Eval在一个ItemTemplate来访问,而不是将其分配给一个BoundField的嵌套属性。

One must instead use DataBinder.Eval in an itemtemplate to access the nested property instead of assigning it to a boundfield.

例如:

<asp:TemplateField>
    <itemtemplate>
        <p><%#DataBinder.Eval(Container.DataItem, "NestedClass.Name")%></p>
    </itemtemplate>
</asp:TemplateField>

另外,你可以创建一个继承的BoundField并重写的GetValue使用的DataBinder.Eval自定义类,因为在这个博客帖子描述:

Alternatively, you can create a custom class which inherits BoundField and overrides GetValue to use DataBinder.Eval, as described in this blog post:

<一个href=\"http://web.archive.org/web/20120121123301/http://iridescence.no/post/FixingBoundFieldSupportforCompositeObjects.aspx\">http://web.archive.org/web/20120121123301/http://iridescence.no/post/FixingBoundFieldSupportforCompositeObjects.aspx

这篇关于GridView控件绑定嵌套类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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