将Entity Framework用作DataGridView的数据源的正确方法是什么?

我尝试通过DataGridView Designer设置DataSource但它没有在那里列出然后我通过生成DataSet的向导生成了新的数据源。

在此处输入图像描述

但是现在我的项目+ DataSet中有Entity Framework我怎么才能使用Entity Framework …我很困惑请帮忙

artiklBindingSource是自动生成的我只想使用EF作为数据源我现在卡在了不需要的DataSet和一大堆乱七八糟的东西。

要在DataGridView任务面板中添加要与DataGridView一起使用的数据源,请打开“选择数据源:combobox”,然后:

  1. 单击“ 添加项目数据源”以打开“ 数据源配置向导”
  2. “选择数据源类型 ”中选择“ 对象”并单击“ 下一步
  3. 在“ 选择数据源对象”中,选择要添加到数据源的类,然后单击“ 完成”
  4. 它将为您的Form添加一个BindingSource ,用作DataGridView的 DataSource ,您应该加载数据并将数据设置为BindingSourc的 DataSource ,然后数据将显示在您的网格中。 例如,加载数据。

这是代码示例:

 using System; using System.Windows.Forms; using System.Data.Entity; namespace WindowsFormsApplication { public partial class Form1 : Form { public Form1() { InitializeComponent(); } SampleDBEntities db; private void Form1_Load(object sender, EventArgs e) { SampleDBEntities db = new SampleDBEntities(); db.Products.Load(); this.productBindingSource.DataSource = db.Products.Local.ToBindingList(); } private void SaveButton_Click(object sender, EventArgs e) { db.SaveChanges(); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { db.Dispose(); } } }