你如何绑定一个GridView列一个子类值? [英] How do you bind a gridview column to a subclass value?

查看:135
本文介绍了你如何绑定一个GridView列一个子类值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我试图绑定到一个ASP.net GridView控件。我的数据源具有收藏,我绑定到列2是一个子类的一部分。我的数据源有一个叫做员工包含员工信息的子类。在绑定列SurveyID和NumberOfExceptions绑定罚款,但Staff.Name和Staff.Office不能绑定。结果

I have a ASP.net gridview that I am trying bind to. My DataSource has a collection and 2 of the columns I am binding to are part of a subclass. My DataSource has a subclass called Staff the contains the staff information. The boundfields SurveyID and NumberOfExceptions bind fine, but the Staff.Name and Staff.Office cannot be bound.

ASP:BoundField的数据字段=SurveyID的HeaderText =ID......结果
ASP:BoundField的数据字段=Staff.Name的HeaderText =姓名......结果
ASP:BoundField的数据字段=Staff.Office的HeaderText =办公室......结果
ASP:BoundField的数据字段=NumberOfExceptions的HeaderText =例外...

asp:BoundField DataField="SurveyID" HeaderText="ID" ...
asp:BoundField DataField="Staff.Name" HeaderText="Name" ...
asp:BoundField DataField="Staff.Office" HeaderText="Office" ...
asp:BoundField DataField="NumberOfExceptions" HeaderText="Exceptions" ...

而code后面的是:

uxSurveyGrid.DataSource = searchResults;
uxSurveyGrid.DataBind();

如果我在code型的SearchResult [0] .Staff.Name后面我可以看到的价值,为什么不能够在GridView评估Staff.Name运行?

If I type searchResults[0].Staff.Name in the code behind I can see the value, why is the runtime not being able to evaluate Staff.Name in the gridview?

你怎么列绑定到子类值?我必须做在codebehind?

How do you bind the columns to the subclass values? Do I have to do it in codebehind?

任何帮助将是AP preciated,

Any help would be appreciated,

标记。

推荐答案

我相信你能得到这个使用模板领域,工作的标记的scriptlet ...

I believe you can get this to work using a Template field and a markup scriptlet...

    <asp:TemplateField>
        <ItemTemplate>
            <asp:Label Id="lblSubclassVal" runat="server" Text="<%# DataBinder.Eval(Container.DataItem, "SubClass.PropertyName")%>"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

这篇关于你如何绑定一个GridView列一个子类值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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