简体   繁体   English

将数据从Entity Framework模型绑定到DataGridView C#

[英]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. 我无法将MySQL数据库中的某些数据绑定到C#Windows窗体中的DataGridView。

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. 我有两个表,由一个表中的外键引用,我想在两个表之间进行JOIN并将其绑定到我的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). 我尝试从DataSet添加一个新的数据源,但这只允许在datagridview中添加1个表(据我所知)。

Next I wanted to create a Entity Framework model and bind that to the DataGridView. 接下来,我想创建一个Entity Framework模型并将其绑定到DataGridView。 I have created a new model named Model1 , but the model is not accessible anywhere in my code. 我创建了一个名为Model1的新模型,但我的代码中的任何地方都无法访问该模型。 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). 我还尝试(在创建模型之后)从对象创建DataSource(尝试选择模型作为对象,但在向导中不可见)。

Or is there another way to bind a simple LEFT JOIN into a DataGridView? 或者是否有另一种方法将简单的LEFT JOIN绑定到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 : Model1是您的EF模型的名称而不是您的上下文类,因此在EF(Model1.Designer.cs)生成的代码中,您必须找到一个继承自ObjectContext的类,例如:

public partial class Entities : ObjectContext

and then: 接着:

new Entities().Table1....

One way you can do that by applying LINQ. 一种方法是通过应用LINQ来做到这一点。 Its same as EF but gives you more levity in between. 它与EF相同但在两者之间给你更多的轻松。 Hope this helps! 希望这可以帮助!

You can write lamba expressions which perform Left Outer Join to your tables. 您可以编写lamba表达式,对表执行Left Outer Join。

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

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