我正在通过实体框架检索数据。在用户表内部有一个角色的导航属性。内部角色有一个属性角色名称。但是当它绑定到网格并使用Eval调用绑定表达式时,我收到以下错误:
DataBinding: Eval("Role.RoleName") is not a valid indexed expression.
我的代码是:
Entities.VSTMEntities vstmEntities = new Entities.VSTMEntities();
var lstUser = (from e in vstmEntities.Users
select e).ToList();
gvUserInformation.DataSource = lstUser;
gvUserInformation.DataBind();
和aspx:
<asp:GridView ID="gvUserInformation" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Username" HeaderText="UserName" />
<asp:BoundField DataField="Password" HeaderText="Password" />
<asp:BoundField DataField="Email" HeaderText="Email Address" />
<asp:BoundField DataField="User_Status" HeaderText="User Status" />
<asp:BoundField DataField="Eval("Role.RoleName")" HeaderText="User Role" />//This causing error
</Columns>
</asp:GridView>
答案 0 :(得分:0)
这就是我要做的事情:
<asp:gridview id="gvUserInformation" runat="server" autogeneratecolumns="False">
<Columns>
<asp:BoundField DataField="Username" HeaderText="UserName" />
<asp:BoundField DataField="Password" HeaderText="Password" />
<asp:BoundField DataField="Email" HeaderText="Email Address" />
<asp:BoundField DataField="User_Status" HeaderText="User Status" />
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="grid2" runat="server" AutoGenerateColumns="False" Width="100%">
<Columns>
<asp:BoundField DataField="RoleName" HeaderText="User Role" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
代码背后的代码:
protected override void OnInit(EventArgs e)
{
gvUserInformation.RowDataBound += gvUserInformation_RowDataBound;
}
void gvUserInformation_RowDataBound(object sender, GridViewRowEventArgs e)
{
var grid2 = (GridView)e.Item.FindControl("grid2");
grid2.DataSource = Role.Where(w => w.RoleName = (e.Item.DataItem as Roles).RoleName);
grid2.Bind();
}