从实体框架上下文查询自动填充控件

发布于 2024-10-11 14:39:54 字数 971 浏览 2 评论 0原文

我有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡寂寞咖啡 2024-10-18 14:39:54

您是否尝试过创建 bindingsource 然后将您的文本框绑定到此。

在运行时,您可以将 EF 实体绑定到绑定源,并且您的控件将自动填充。

可以找到一个很好的示例 这里 - 它显示了到网格的绑定,但其原理与绑定到单个控件类似。

此外,要拉取单个实体,您可以执行以下操作:

AccountMember member = context.AccountMember
                              .Single(m => m.AccountMemberId == ModifyUser);

// Bind the fetched entity to the bindingsource and hence to the UI controls
// At runtime this is all that is needed to update the controls, as long as 
// you have set things up at design time.
myBindingSource.DataSource = member;

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:

AccountMember member = context.AccountMember
                              .Single(m => m.AccountMemberId == ModifyUser);

// Bind the fetched entity to the bindingsource and hence to the UI controls
// At runtime this is all that is needed to update the controls, as long as 
// you have set things up at design time.
myBindingSource.DataSource = member;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文