locked
How do i show navigation property asp.net gridview entity framework 5 RRS feed

  • Question

  • User36582938 posted

    I have a navgation point of team in players and I want to be able to show the team name rather than its id value in my gridview. please dont attempt to ask if you dont no entity framework.

      <asp:EntityDataSource ID="entPlayers"  runat="server" ConnectionString="name=soccerEntities" DefaultContainerName="soccerEntities" EnableFlattening="False" EntitySetName="players" Select="it.[Name], it.[player_id], it.[address], it.[town], it.[gender], it.[zipcode], it.[telephone], it.[email], it.[active], it.[createdDate], it.[dob], it.[teamId], it.[id]"></asp:EntityDataSource>
        <asp:GridView ID="grdSoccerPlayers"  runat="server"  CssClass= "table table-striped table-bordered table-condensed"  DataKeyNames="id" AutoGenerateColumns="False"  Visible="true" OnRowCommand="grdSoccerPlayers_RowCommand" OnRowUpdating="grdSoccerPlayers_RowUpdating">
        <EmptyDataTemplate
            >
           <div class="alert alert-info alert">
                          <h4><i class="icon fa fa-info"></i> Alert!</h4>
                        Their are no players in the system. No User has registered as a player.
                      </div>
    
    
        </EmptyDataTemplate>
                <Columns>
                     <asp:TemplateField Visible="false">
                    <ItemTemplate>
                        <asp:Label ID="lblID"
                            Text='<%# Eval("teamId") %>'
                            runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                     <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lblName"
                            Text='<%# Eval("residentName") %>'
                            runat="server" />
                    </ItemTemplate>
                         </asp:TemplateField>
                <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
                <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
                <asp:BoundField DataField="player_id" HeaderText="Player Id" ReadOnly="True" SortExpression="player_id" />
                 <asp:BoundField DataField="dob" HeaderText="DOB" ReadOnly="True" SortExpression="dob" />
               
                <asp:BoundField DataField="address" HeaderText="Address" ReadOnly="True" SortExpression="address" />
                <asp:BoundField DataField="town" HeaderText="Town" ReadOnly="True" SortExpression="town" />
                <asp:BoundField DataField="gender" HeaderText="Gender" ReadOnly="True" SortExpression="gender" />
                <asp:BoundField DataField="zipcode" HeaderText="Zip Code" ReadOnly="True" SortExpression="zipcode" />
                  <asp:CheckBoxField DataField="active" HeaderText="Active" ReadOnly="True" SortExpression="active" />
                <asp:BoundField DataField="createdDate" HeaderText="Date" ReadOnly="True" SortExpression="createdDate" />
                <asp:BoundField DataField="teamId" HeaderText="teamId" ReadOnly="True" SortExpression="teamId" />
                  <asp:ButtonField CommandName="Edit" Text="Edit" />
            <asp:ButtonField CommandName="Delete" Text="Delete" />
            </Columns>
                  </asp:GridView>
    
         

    And here is my edmx showing the navigation point my question is how do i show the team name instead of the id ?.

    I tried the following

      protected void grdSoccerPlayers_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                // This is for retrieving the entity object for the resident selected
                Label idLabel = (Label)grdSoccerPlayers.Rows[e.RowIndex].Cells[1].FindControl("lblID");
                Guid teamid =  new Guid(idLabel.Text);
    
                team teamName = _dal.SoccerEntities.teams.Where(res => res.id == teamid).First();
                
                     Label lblName = (Label)grdSoccerPlayers.Rows[e.RowIndex].Cells[2].FindControl("lblName");
    
                     lblName.Text = teamName.name;
            }



    And here you see im trying to use a template field but its not updating it

     <asp:GridView ID="grdSoccerPlayers"  runat="server"  CssClass= "table table-striped table-bordered table-condensed"  DataKeyNames="id" AutoGenerateColumns="False"  Visible="true" OnRowCommand="grdSoccerPlayers_RowCommand" OnRowUpdating="grdSoccerPlayers_RowUpdating" OnDataBinding="grdSoccerPlayers_DataBinding">
        <EmptyDataTemplate
            >
           <div class="alert alert-info alert">
                          <h4><i class="icon fa fa-info"></i> Alert!</h4>
                        Their are no players in the system. No User has registered as a player.
                      </div>
    
    
        </EmptyDataTemplate>
                <Columns>
                     <asp:TemplateField Visible="false">
                    <ItemTemplate>
                        <asp:Label ID="lblID"
                            Text='<%# Eval("teamId") %>'
                            runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                     <asp:TemplateField HeaderText="Team Name">
                    <ItemTemplate>
                        <asp:Label ID="lblName"
                            Text=''
                            runat="server" />
                    </ItemTemplate>
                         </asp:TemplateField>
                <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
                <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
                <asp:BoundField DataField="player_id" HeaderText="Player Id" ReadOnly="True" SortExpression="player_id" />
                 <asp:BoundField DataField="dob" HeaderText="DOB" ReadOnly="True" SortExpression="dob" />
               
                <asp:BoundField DataField="address" HeaderText="Address" ReadOnly="True" SortExpression="address" />
                <asp:BoundField DataField="town" HeaderText="Town" ReadOnly="True" SortExpression="town" />
                <asp:BoundField DataField="gender" HeaderText="Gender" ReadOnly="True" SortExpression="gender" />
                <asp:BoundField DataField="zipcode" HeaderText="Zip Code" ReadOnly="True" SortExpression="zipcode" />
                  <asp:CheckBoxField DataField="active" HeaderText="Active" ReadOnly="True" SortExpression="active" />
                <asp:BoundField DataField="createdDate" HeaderText="Date" ReadOnly="True" SortExpression="createdDate" />
                <asp:BoundField DataField="teamId" HeaderText="teamId" ReadOnly="True" SortExpression="teamId" />
                  <asp:ButtonField CommandName="Edit" Text="Edit" />
            <asp:ButtonField CommandName="Delete" Text="Delete" />
            </Columns>
                  </asp:GridView>
    
         
         

    Monday, November 30, 2015 10:14 PM

Answers