简体   繁体   中英

Bind data from Entity Framework model to DataGridView C#

I am having trouble binding some data from my MySQL database to a DataGridView in C# Windows Forms.

I have 2 tables, referenced by a foreign key in one table, and I want to do a JOIN between the 2 tables and bind that to my DataGridView. I have tried adding a new datasource from a DataSet, but that only allows to add 1 table in the datagridview (as far as I can tell).

Next I wanted to create a Entity Framework model and bind that to the DataGridView. I have created a new model named Model1 , but the model is not accessible anywhere in my code. Is there something wrong with my logic?

I have also tried (after creating the model) to create a DataSource from an object (trying to select the model as the object, but it wasn't visible in the wizard).

Or is there another way to bind a simple LEFT JOIN into a DataGridView?

Model1 is name of your EF Model not your context class,So in generated code by EF(Model1.Designer.cs), you must find a class that inherit from ObjectContext such as :

public partial class Entities : ObjectContext

and then:

new Entities().Table1....

One way you can do that by applying LINQ. Its same as EF but gives you more levity in between. Hope this helps!

You can write lamba expressions which perform Left Outer Join to your tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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