0

I am retrieving data through entity framework. Inside User Table there is a navigation property of role. Inside role there is a property role name. But when binding it to grid and calling the binding expression using Eval i am getting the following error:

DataBinding: Eval("Role.RoleName") is not a valid indexed expression.

My code is:

  Entities.VSTMEntities vstmEntities = new Entities.VSTMEntities();

        var lstUser = (from e in vstmEntities.Users
                       select e).ToList();

        gvUserInformation.DataSource = lstUser;
        gvUserInformation.DataBind();

and 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(&quot;Role.RoleName&quot;)" HeaderText="User Role" />//This causing error
                       </Columns>
                   </asp:GridView>
Hassaan
  • 3,931
  • 11
  • 34
  • 67
  • possible duplicate of [GridView bound with with Properties of nested class](http://stackoverflow.com/questions/1130351/gridview-bound-with-with-properties-of-nested-class) – deostroll Jun 26 '14 at 12:11

1 Answers1

0

This is what I would do :

<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>

code behind :

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();
}
Krunal Patil
  • 3,666
  • 5
  • 22
  • 28