c# – 数据绑定linq查询到Entity Framework 5.0中的datagridView

我正在学习实体框架(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();

        }
    }

解决方法

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,r.ClockOut }).ToList();

dataGridView1.DataSource = bindingSource1;

见:msdn documentation

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么