C# 在 Entity Framework 5.0 中将 linq 查询数据绑定到 datagridView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12501183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Data binding linq query to datagridView in Entity Framework 5.0
提问by Mr1159pm
I am learning the Entity Framework (5.0 and VSExpress 2012) and I'm having real trouble binding my query to a dataGridView in WinForms. I have the below code and it displays my query okay when I start the application but I don't know what I need to do to update the dataGridView after changing the data in the underlying database. What's the best way of doing this? What am I doing wrong here?
我正在学习实体框架(5.0 和 VSExpress 2012)并且我在将查询绑定到 WinForms 中的 dataGridView 时遇到了真正的问题。我有下面的代码,当我启动应用程序时,它显示我的查询正常,但我不知道在更改基础数据库中的数据后我需要做什么来更新 dataGridView。这样做的最佳方法是什么?我在这里做错了什么?
private void Form1_Load(object sender, EventArgs e)
{
using( var ctx = new TimeKeepEntities())
{
var qLoggedIn = from r in ctx.tblTimeRecords
where (r.tblEmployee.Active && !r.ClockOut.HasValue) || System.Data.Objects.EntityFunctions.DiffDays(r.ClockOut, DateTime.Now)<30
select new { Name = r.tblEmployee.Last + ", " + r.tblEmployee.First, r.tblProject.ProjName, r.ClockIn, r.ClockOut };
dataGridView1.DataSource = qLoggedIn.ToList();
}
}
采纳答案by Lance
Pho please note that they are using winforms not asp.net. According to MSDN you can do the following:
Pho 请注意,他们使用的是 winforms 而不是 asp.net。根据 MSDN,您可以执行以下操作:
BindingSource bindingSource1 = new BindingSource();
bindingSource1.DataSource = (from r in ctx.tblTimeRecords
where (r.tblEmployee.Active && !r.ClockOut.HasValue) || System.Data.Objects.EntityFunctions.DiffDays(r.ClockOut, DateTime.Now)<30
select new { Name = r.tblEmployee.Last + ", " + r.tblEmployee.First, r.tblProject.ProjName, r.ClockIn, r.ClockOut }).ToList();
dataGridView1.DataSource = bindingSource1;
see: msdn documentation
请参阅:msdn 文档
回答by pho
You have to bind the data with dataGridView1.DataBind();:
您必须使用以下方式绑定数据dataGridView1.DataBind();:
...
dataGridView1.DataSource = qLoggedIn.ToList();
dataGridView1.DataBind();
...
回答by Jim Wooley
.Net uses a disconnected model. When you fetch information from the database, it is a snapshot at that point in time. If you change data in the underlying data store, those changes won't be reflected unless you explicitly re-query the database and rebind your UI.
.Net 使用断开连接的模型。当您从数据库中获取信息时,它是该时间点的快照。如果您更改底层数据存储中的数据,除非您明确重新查询数据库并重新绑定您的 UI,否则这些更改将不会被反映。
When you save changes in your UI, EF can check to see if anyone else changed the row that you are modifying (for concurrency concerns) and let you know if there is a potential conflict.
当您在 UI 中保存更改时,EF 可以检查是否有其他人更改了您正在修改的行(出于并发问题),并让您知道是否存在潜在冲突。
回答by JJorian
IEnumerable<DataRow> query =( from p in orginalList.AsEnumerable()
where p.Field<long>("Category") == 2
select p).ToList();
DataTable boundTable = query.CopyToDataTable<DataRow>();
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = boundTable;

