简体   繁体   English

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

[英]How to add items to Combobox from Entity Framework?

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.我尝试了很多解决方案,但没有一个有效。

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?).如果这不起作用,那么列名可能与“名称”不同(也许是“名称”?)。

If you use "using" you need to place a ToList() for evaluate before close connection.如果您使用“使用”,则需要在关闭连接之前放置一个 ToList() 进行评估。 use ItemsSource , ValueMember and DisplayMember are case sensitive使用 ItemsSource 、 ValueMember 和 DisplayMember 区分大小写

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

Hope this help.希望这有帮助。

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;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM