19

I tried setting DataSource via DataGridView Designer but it wasn't listed there and then I generated new datasource via wizard which generated DataSet.

enter image description here

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.

2 Answers 2

24

To add a data source to use with your DataGridView in DataGridView Tasks panel, open Choose Data Source: combo box and then:

  1. Click Add Project Data Source to open Data Source Configuration Wizard.
  2. In Choose a Data Source Type Choose Object and click Next.
  3. In Select Data Source Objects choose class which you want to add to data source and click Finish.
  4. It will add a BindingSource to your Form which is used as DataSource of your DataGridView and you should load data and set data to DataSource of your BindingSourc and then data will be shown in your grid. For example to load data.

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();
        }
    }
}
5
  • 1
    Thank you for taking time to answer, I always chose Database as Data Source and not Object I think this might be the thing that troubled me...
    – solujic
    Feb 9, 2017 at 9:14
  • You may also want to just use "ToList" instead of "ToBindingList". Running faster for me and a binding list is created internally anyway. See this stackoverflow answer for the difference: stackoverflow.com/questions/2243950/…
    – AS7K
    Nov 16, 2018 at 15:44
  • How can a filter be applied to the entitylist, by field "ProductName" in example?
    – Bull
    Nov 19, 2021 at 18:32
  • 1
    @Bull You may want to take a look at this post or this one. Nov 19, 2021 at 22:01
  • Thank you very much, Reza!! Already saw those posts and others and found myself the soluction. Thumbs up!!
    – Bull
    Nov 20, 2021 at 11:59
4

Don't know if it's the fastest method but it's the simpler:

dataGridViewStudents.DataSource = schoolContext.Students.ToList<Student>();
1
  • 4
    It will work but the DbContext cannot track changes that way, you should use schoolContext.Students.Load(); dataGridViewStudents.DataSource = schoolContext.Students.Local.ToBindingList(); Mar 9, 2019 at 1:28

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.