从实体框架上下文查询自动填充控件
我有以下代码:
IList<AccountMember> query;
using (DBEntities context = new DBEntities())
{
Guid ModifyUser = new Guid(Session["ModifyUser"].ToString());
query = (from AccountMember member in context.AccountMember
where member.AccountMemberId == ModifyUser
select member).ToList();
foreach (AccountMember member in query)
{
//this.FirstName.Text = member.FirstName;
ControlCollection controls = this.Controls;
foreach (Control control in controls)
{
if (control is TextBox)
{
TextBox x = (TextBox)control;
x.Text = member.FirstName; // want to replace the .FirstName with the TextBox ID value somehow
}
} // foreach (Control control in controls)
} // foreach (AccountMember member in query)
} // using (DBEntities context = new DBEntities())
在包含 x.Text = member.FirstName; 的行处我想用 TextBox ID 字符串替换 FirstName 项。这样我就可以自动循环并填充我的文本框
I have the following code:
IList<AccountMember> query;
using (DBEntities context = new DBEntities())
{
Guid ModifyUser = new Guid(Session["ModifyUser"].ToString());
query = (from AccountMember member in context.AccountMember
where member.AccountMemberId == ModifyUser
select member).ToList();
foreach (AccountMember member in query)
{
//this.FirstName.Text = member.FirstName;
ControlCollection controls = this.Controls;
foreach (Control control in controls)
{
if (control is TextBox)
{
TextBox x = (TextBox)control;
x.Text = member.FirstName; // want to replace the .FirstName with the TextBox ID value somehow
}
} // foreach (Control control in controls)
} // foreach (AccountMember member in query)
} // using (DBEntities context = new DBEntities())
At the line containing x.Text = member.FirstName; I would like to replace the FirstName item with the TextBox ID string. So that I can just loop and populate my TextBoxes automatically
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过创建
bindingsource
然后将您的文本框绑定到此。在运行时,您可以将 EF 实体绑定到绑定源,并且您的控件将自动填充。
可以找到一个很好的示例 这里 - 它显示了到网格的绑定,但其原理与绑定到单个控件类似。
此外,要拉取单个实体,您可以执行以下操作:
Have you tried creating a
bindingsource
then binding your text boxes to this.At runtime you can then bind your EF entity to the binding source and your controls will be autopopulated.
A nice example can be found here - it shows binding to a grid, but the principle is similar for binding to individual controls.
Additionally to pull a single entity you can do: