1

I don't know how to use DbContext to show data in a Wpf Data Control as the ListView, ListBox or DataGrid.

I'm using the Database first approach, with VS2012.

I have a Database with one table (the simplest case).

enter image description here

Then I added an ADO Net Entity Data Model, it added several classes to my project, among them is OneTableEntities : DbContext:

enter image description here

Now I have a ListBox in a Wpf Window, what do I bind the listbox.ItemsSource to? I tried "listBox.ItemsSource = context.MyTables;" but I get this error:

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList().

Where is that Load operation?, How can I populate a local store?

1 Answer 1

3

As error message says, you cannot bind control to query. Use ToList() to execute database query and save results to in-memory collection. Then you will be able to bind this collection to listbox:

listBox.ItemsSource = context.MyTables.ToList();

Another option you can find directly in error message:

context.MyTables.Load(); 
listBox.ItemsSource = context.MyTables.Local; 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.