51564

How to add items to Combobox from Entity Framework?

Question:

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 <strong>Model.Customer</strong>

What is the problem??

I tried many solution but non of them are working.

Answer1:

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?).

Answer2:

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

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

Hope this help.

Answer3:

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

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;

Recommend

  • SQL Column to TextBox (from ComboBox)
  • Entity Framework 4 Databinding on WinForms ComboBoxes with Foreign Keys
  • Trying to bind XML to a ComboBox using WinForm and C#
  • How do I insert a record into multiple tables in a database
  • Search in A ListBox
  • Binding Source doesn't work on column names with two or more words in DataGridView C#
  • Dataset column name and row value
  • filtering any columns of a database table client T-SQL select from case when then where
  • WCF web service returning json format data
  • Need help with Eval inside DataList
  • Why does Apple use assign rather than weak to store a delegate?
  • Table striping rows in CSS Grid
  • How to check if a string contains any string of a column in MySQL and vice versa?
  • Is is safe to use HSQLDB for production? (JBoss AS5.1)
  • Is there a way to disable a specific event in kendo ui scheduler?
  • How to Add Polymorphic Comments to Feed?
  • Oracle - Second level subquery cannot see field from main query
  • Row to Column conversion in Talend
  • Merging rows to columns
  • Low TTL with Leveled Compaction, should I reduce gc_grace_seconds to improve read performance withou
  • List images(01.png) and descriptions(01.txt) from directory
  • Limiting recursion to certain level - Duplicate rows
  • Word Open XML Mail Merge
  • How do I alternate colors in Flat List (React Native)
  • D3 nodes and links from JSON with nested arrays of children
  • Retrieving value from sql ExecuteScalar()
  • Spring security and special characters
  • Trying to switch camera back to front but getting exception
  • vba code to select only visible cells in specific column except heading
  • Do I've to free mysql result after storing it?
  • Transpose CSV data with awk (pivot transformation)
  • Load html files in TinyMce
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Programmatically clearing map cache
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • Why do underscore prefixed variables exist?