Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to use Entity Framework as datasource for DataGridView?

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.

like image 235
solujic Avatar asked Feb 08 '17 13:02

solujic


People also ask

What is datasource in DataGridView?

The DataGridView class supports the standard Windows Forms data-binding model. This means the data source can be of any type that implements one of the following interfaces: The IList interface, including one-dimensional arrays. The IListSource interface, such as the DataTable and DataSet classes.

How retrieve data from database and display in DataGridView in C#?

Step 1: Make a database with a table in SQL Server. Step 2: Create a Windows Application and add DataGridView on the Form. Now add a DataGridView control to the form by selecting it from Toolbox and set properties according to your needs.

What is DataGridView tool how you can use it?

The DataGridView control provides a powerful and flexible way to display data in a tabular format. You can use the DataGridView control to show read-only views of a small amount of data, or you can scale it to show editable views of very large sets of data.


2 Answers

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();
        }
    }
}
like image 159
Reza Aghaei Avatar answered Oct 18 '22 09:10

Reza Aghaei


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

dataGridViewStudents.DataSource = schoolContext.Students.ToList<Student>();
like image 21
Kethav Avatar answered Oct 18 '22 10:10

Kethav