C# 如何从实体框架向 Combobox 添加项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11745714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:04:27  来源:igfitidea点击:

How to add items to Combobox from Entity Framework?

c#entity-frameworkcombobox

提问by Saleh

I have this code:

我有这个代码:

    private void FillCombobox()
    {
        using (InventoryEntities c = new InventoryEntities(Properties.Settings.Default.Connection))
        {
            List<Customer> usepurposes = c.Customers.ToList();
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            foreach (Customer usepurpose in usepurposes)
            {
                dt.Rows.Add(usepurpose.id, usepurpose.name);
            }
            comboBox1.ValueMember = dt.Columns[0].ColumnName;
            comboBox1.DisplayMember = dt.Columns[1].ColumnName;
            comboBox1.DataSource = dt;

        }
    }

and I call this method in:

我把这个方法称为:

    private void frmBillIn_Load(object sender, EventArgs e)
    {
        FillCombobox();
    }

When I run my app, combobox will not display customers(items).

当我运行我的应用程序时,组合框不会显示客户(项目)。

just display Model.Customer

只显示Model.Customer

What is the problem??

问题是什么??

I tried many solution but non of them are working.

我尝试了很多解决方案,但没有一个有效。

采纳答案by Wiktor Zychla

You don't have to mix two worlds, the world of Entity Framework and the world of DataSets. Bind directly:

您不必混合两个世界,实体框架的世界和数据集的世界。直接绑定:

    using (InventoryEntities c = new InventoryEntities(Properties.Settings.Default.Connection))
    {
        comboBox1.DataSource    = c.Customers;
        comboBox1.ValueMember   = "id";
        comboBox1.DisplayMember = "name";
    }

If this does not work, then the column name could be different from "name" ("Name" perhaps?).

如果这不起作用,那么列名可能与“名称”不同(也许是“名称”?)。

回答by Xilmiki

If you use "using" you need to place a ToList() for evaluate before close connection. use ItemsSource , ValueMember and DisplayMember are case sensitive

如果您使用“使用”,则需要在关闭连接之前放置一个 ToList() 进行评估。使用 ItemsSource 、 ValueMember 和 DisplayMember 区分大小写

using (InventoryEntities c = new InventoryEntities())
    {
        comboBox1.ItemsSource   = c.Customers.toList();
        comboBox1.ValueMemberPath   = "Id";
        comboBox1.DisplayMemberPath = "Name";
    }

Hope this help.

希望这有帮助。

回答by Raj

Refer following sample. (name references => DAL=Data access layer, projectEntities = entity set name) Hope this will help..

请参阅以下示例。(名称引用 => DAL=数据访问层,projectEntities = 实体集名称)希望这会有所帮助..

List itemsList = new List();

List itemsList = new List();

            using (DAL.projectEntities en = new DAL.projectEntities())
            {
                foreach (var item in en.tableName.Where(a => a.tableName != null).ToList())
                {
                    itemsList.Add(item.tableFieldName);
                }                                   
            }

            comboboxTable.ItemsSource = itemsList;

回答by pat capozzi

In the current version of WinForms I have found this to work

在当前版本的 WinForms 中,我发现它可以工作

RentalsEntities1 db = new RentalsEntities1();
        List<SiteSeeingLocation> ssloc = db.SiteSeeingLocations.ToList();
        cbSites.DataSource = ssloc;
        cbSites.ValueMember = "id";
        cbSites.DisplayMember = "ssLocation";