Elad Lachmi
Elad Lachmi

Reputation: 10561

asp.net - Bind to a linq datasource and add external column

I have a databound control which displays a grid. The grid is bound to a linq to sql data source. I use the following code:

PaymentsDataContext data = new PaymentsDataContext();
            var q = from act in data.activations
                    where act.approved != true
                    orderby act.activationDate ascending
                    select new {activationID = act.activationID, userName = act.userName,
                    brokerName = act.broker.brokerName, existingAccount = act.existingAccount,
                    activationDate = act.activationDate, brokerUser = act.brokerUser, approved = act.approved};
            activationPending.DataSource = q;
            activationPending.DataBind();

I want to add another column to the grid. I want to show the user's email address. I get it like so:

var member = System.Web.Security.Membership.GetUser(username);
string email = member.Email;

How could I add this as a field in the grid, since it's not in the Payment DB at all?

Upvotes: 3

Views: 1726

Answers (2)

Yahia
Yahia

Reputation: 70369

try this:

var q = from act in data.activations
                    where act.approved != true
                    orderby act.activationDate ascending
                    select new {activationID = act.activationID, userName = act.userName,
                    brokerName = act.broker.brokerName, 
                    existingAccount = act.existingAccount,
                    activationDate = act.activationDate, brokerUser = act.brokerUser,
                    approved = act.approved, 
                    email = System.Web.Security.Membership.GetUser(act.userName).Email };

since you already bin q and not data.activations this adds the external column to the grid.

Edit : Because of the new added column your template of the grid view must have a place to accept it you can add this manually like :

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
<Columns>
.
.
<asp:BoundField DataField="email" HeaderText="email" 
            SortExpression="email" />
.
.
</Columns>
 </asp:GridView>

or set the property of the gridview named AutoGenerateColumns to true

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
    </asp:GridView> 

Upvotes: 2

Peyman
Peyman

Reputation: 3138

you can write the Extension method and use it in your select. Check this topic

 private Func<DataClasses.Activations, String> GetUserEmail(string username)
 {
    return System.Web.Security.Membership.GetUser(username).Email;
 }

 PaymentsDataContext data = new PaymentsDataContext();
        var q = from act in data.activations
                where act.approved != true
                orderby act.activationDate ascending
                select new {activationID = act.activationID, userName = act.userName,
                brokerName = act.broker.brokerName, existingAccount = act.existingAccount,
                activationDate = act.activationDate, brokerUser = act.brokerUser, approved = act.approved, 
                Email = GetUserEmail(act.username)};

Upvotes: 0

Related Questions