gpt4 book ai didi

c# - asp.net - 绑定(bind)到 linq 数据源并添加外部列

转载 作者:行者123 更新时间:2023-11-30 17:17:07 25 4
gpt4 key购买 nike

我有一个显示网格的数据绑定(bind)控件。网格绑定(bind)到 linq to sql 数据源。我使用以下代码:

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();

我想在网格中添加另一列。我想显示用户的电子邮件地址。我是这样理解的:

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

我怎么能把它作为一个字段添加到网格中,因为它根本不在支付数据库中?

最佳答案

试试这个:

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

因为您已经将 q 而不是 data.activations 添加到网格中,所以这会将外部列添加到网格中。

编辑:由于新添加的列,您的 GridView 模板必须有一个位置要接受它,您可以手动添加它,例如:

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

或者将名为 AutoGenerateColumns 的 gridview 的属性设置为 true

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

关于c# - asp.net - 绑定(bind)到 linq 数据源并添加外部列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6770361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com