Reputation: 1004
I tried setting DataSource via DataGridView Designer but it wasn't listed there and then I generated new datasource via wizard which generated DataSet.
But now I have Entity Framework in my project + DataSet how can I use Entity Framework only... I'm confused.
artiklBindingSource
was automatically generated I only wanted to use EF as datasource now I'm stuck with unwanted DataSet.
Upvotes: 20
Views: 31146
Reputation: 125227
To add a data source to use with your DataGridView
in DataGridView Tasks panel, open Choose Data Source: combo box and then:
Here is the code sample:
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)
{
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();
}
}
}
Upvotes: 25
Reputation: 41
Don't know if it's the fastest method but it's the simpler:
dataGridViewStudents.DataSource = schoolContext.Students.ToList<Student>();
Upvotes: 4