问题描述
我有一个正在尝试绑定的 ASP.net gridview.我的 DataSource 有一个集合,我绑定到的 2 个列是子类的一部分.我的数据源有一个名为 Staff 的子类,它包含员工信息.绑定字段 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 DataField="SurveyID" HeaderText="ID" ...
asp:BoundField DataField="Staff.Name" HeaderText="Name" ...
asp:BoundField DataField="Staff.Office" HeaderText="Office" ...
asp:BoundField DataField="NumberOfExceptions" HeaderText="Exceptions" ...
后面的代码是:
uxSurveyGrid.DataSource = searchResults;
uxSurveyGrid.DataBind();
如果我在后面的代码中输入 searchResults[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?
如何将列绑定到子类值?我是否必须在代码隐藏中执行此操作?
How do you bind the columns to the subclass values? Do I have to do it in codebehind?
任何帮助将不胜感激,
标记.
推荐答案
我相信您可以使用模板字段和标记脚本来使其工作...
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 列绑定到子类值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!