绑定到linq数据源并添加外部列
本文关键字:添加 外部 数据源 linq 绑定 | 更新日期: 2023-09-27 18:03:12
我有一个显示网格的绑定控件。网格被绑定到一个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;
我怎么能在网格中添加这个字段,因为它不是在支付数据库在所有?
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 };
由于您已经存储了q
而不是data.activations
,因此将外部列添加到网格中。
编辑:由于新添加的列,您的网格视图模板必须有一个位置要接受它,您可以像这样手动添加:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
<Columns>
.
.
<asp:BoundField DataField="email" HeaderText="email"
SortExpression="email" />
.
.
</Columns>
</asp:GridView>
或将名为autogeneratecolns的gridview的属性设置为true
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
</asp:GridView>
您可以编写Extension方法并在选择中使用它。检查这个主题
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)};