![](/img/trans.png)
[英]How to use Entity Framework as generic DataSource for DataGridView?
[英]What is the correct way to use Entity Framework as datasource for DataGridView?
要在DataGridView 任務面板中添加要與DataGridView
一起使用的數據源,請打開選擇數據源:組合框,然后:
這是代碼示例:
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();
}
}
}
不知道這是否是最快的方法,但它更簡單:
dataGridViewStudents.DataSource = schoolContext.Students.ToList<Student>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.