Matt
Matt

Reputation: 4190

Bind column to a different entity source than the gridview is

I have a gridview bound to an Entity DataSource (Permissions). One of the columns lists a UserId, which is the primary key in another table. How do I go about replacing that UserId with the corresponding Name form the Users Entity DataSource?

Upvotes: 0

Views: 2071

Answers (3)

Matt
Matt

Reputation: 4190

Sorted it out..

<asp:TemplateField HeaderText="ApprovedBy" SortExpression="ApprovedBy" >
            <ItemTemplate>   
                <asp:Label ID="lblUser" runat="server"  Text='<%# GetApprover(Eval("ApprovedBy").ToString()) %>'/>
            </ItemTemplate>
</asp:TemplateField>

and code behind:

protected string GetApprover(string ApproverId)
    {
        int approver = Convert.ToInt32(ApproverId);
        var approversList = (from a in EntUsers.Users
                             where a.UserId == approver
                            select a.Name).FirstOrDefault();

        return approversList;
    }

Upvotes: 0

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

You can try like...

<asp:TemplateField HeaderText="User Name" >
 <ItemTemplate>
   <asp:Label ID="lblUser" runat="server" Text='<%# Eval("ParentEntityName.Name")%>'
 </ItemTemplate>
</asp:TemplateField>

Upvotes: 1

TheGeekYouNeed
TheGeekYouNeed

Reputation: 7539

You can do a lookup in your Users Entity Source the RowDataBound event of the Gridview.

Upvotes: 0

Related Questions