3

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?

Elad Lachmi
  • 10,406
  • 13
  • 71
  • 133

2 Answers2

2

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> 
xsari3x
  • 442
  • 2
  • 12
  • 36
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • I hoped you were right, but no. I get a null object ref exception. – Elad Lachmi Jul 21 '11 at 01:37
  • If act.userName is not guranteed to return an eMail-Adress via GetUser then this can happen... I need to think a bit about this... – Yahia Jul 21 '11 at 01:40
  • It is guranteed. Both username and email address are 'Not NULL' in both the DB itself and the DAL. Even if that statment was not really true, I checked the current table and every user has an email address. – Elad Lachmi Jul 21 '11 at 01:45
0

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)};
Community
  • 1
  • 1
Peyman
  • 3,068
  • 1
  • 18
  • 32
  • This might work, but I must say I'm in over my head with that thread. I don't know linq that well and I have no idea what they are going on about. Any chance you can point me to a specific answer or part of an answer. – Elad Lachmi Jul 21 '11 at 01:47