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.net gridview,我正在尝试绑定。我的DataSource有一个集合,我绑定的2列是子类的一部分。我的DataSource有一个名为Staff的子类,其中包含人员信息。 Boundfields SurveyID和NumberOfExceptions绑定正常,但Staff.Name和Staff.Office无法绑定。
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" ...
And the code behind is:
而背后的代码是:
uxSurveyGrid.DataSource = searchResults;
uxSurveyGrid.DataBind();
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?
如果我在后面的代码中键入searchResults [0] .Staff.Name,我可以看到值,为什么运行时无法在gridview中评估Staff.Name?
How do you bind the columns to the subclass values? Do I have to do it in codebehind?
如何将列绑定到子类值?我是否必须在代码隐藏中执行此操作?
Any help would be appreciated,
任何帮助,将不胜感激,
Mark.
4 个解决方案
#1
8
I believe you can get this to work using a Template field and a markup scriptlet...
我相信你可以使用Template字段和标记scriptlet来实现这一点......
<asp:TemplateField>
<ItemTemplate>
<asp:Label Id="lblSubclassVal" runat="server" Text="<%# DataBinder.Eval(Container.DataItem, "SubClass.PropertyName")%>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
#2
6
The data binding mechanism behind ASP.NET GridView supports only one level bindings. (as opposed to its WinForms Binding counterpart that supports multi-level in the case of binding to a DataSet / DataTable / DataView).
ASP.NET GridView背后的数据绑定机制仅支持一个级别绑定。 (而不是在绑定到DataSet / DataTable / DataView的情况下支持多级的WinForms Binding对应物)。
You have three possible solutions:
您有三种可能的解决方案:
- Handling the ItemDataBound event for each row
- Extending your root level entities with properties that expose the child object properties and using these properties for the binding expressions
- Instead of using a BoundField you could use a Template Field and generate the content using a <%= %> expression that accesses the Data Item.
处理每行的ItemDataBound事件
使用公开子对象属性的属性扩展根级实体,并使用这些属性作为绑定表达式
您可以使用模板字段而不是使用BoundField,并使用访问数据项的<%=%>表达式生成内容。
#3
0
Mark,
I am 99.9% sure that you will have to handle this in the codebehind on the ItemDataBound event for the individual row.
我99.9%肯定你必须在单个行的ItemDataBound事件的代码隐藏中处理这个问题。
Remember you can get the whole databould object from e.Item.DataItem
请记住,您可以从e.Item.DataItem获取整个数据库对象
#4
0
The [Name].[Name] syntax is not supported by BoundField. Only simple property names.
BoundField不支持[Name]。[Name]语法。只有简单的属性名称。
#1
8
I believe you can get this to work using a Template field and a markup scriptlet...
我相信你可以使用Template字段和标记scriptlet来实现这一点......
<asp:TemplateField>
<ItemTemplate>
<asp:Label Id="lblSubclassVal" runat="server" Text="<%# DataBinder.Eval(Container.DataItem, "SubClass.PropertyName")%>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
#2
6
The data binding mechanism behind ASP.NET GridView supports only one level bindings. (as opposed to its WinForms Binding counterpart that supports multi-level in the case of binding to a DataSet / DataTable / DataView).
ASP.NET GridView背后的数据绑定机制仅支持一个级别绑定。 (而不是在绑定到DataSet / DataTable / DataView的情况下支持多级的WinForms Binding对应物)。
You have three possible solutions:
您有三种可能的解决方案:
- Handling the ItemDataBound event for each row
- Extending your root level entities with properties that expose the child object properties and using these properties for the binding expressions
- Instead of using a BoundField you could use a Template Field and generate the content using a <%= %> expression that accesses the Data Item.
处理每行的ItemDataBound事件
使用公开子对象属性的属性扩展根级实体,并使用这些属性作为绑定表达式
您可以使用模板字段而不是使用BoundField,并使用访问数据项的<%=%>表达式生成内容。
#3
0
Mark,
I am 99.9% sure that you will have to handle this in the codebehind on the ItemDataBound event for the individual row.
我99.9%肯定你必须在单个行的ItemDataBound事件的代码隐藏中处理这个问题。
Remember you can get the whole databould object from e.Item.DataItem
请记住,您可以从e.Item.DataItem获取整个数据库对象
#4
0
The [Name].[Name] syntax is not supported by BoundField. Only simple property names.
BoundField不支持[Name]。[Name]语法。只有简单的属性名称。