数据LINQ查询绑定的DataGridView实体框架5.0 [英] Data binding linq query to datagridView in Entity Framework 5.0

查看:120
本文介绍了数据LINQ查询绑定的DataGridView实体框架5.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习实体框架(5.0和VSExpress 2012)和我有真正的麻烦我的查询绑定到的WinForms一个DataGridView。
我有下面的代码,它显示我的查询好吧,当我启动应用程序,但我不知道我需要做底层数据库更改数据后更新的dataGridView。什么是这样做的最佳方式?我在做什么错在这里?



 私人无效Form1_Load的(对象发件人,EventArgs五)
{
&安培;! - [R使用(VAR CTX =新TimeKeepEntities())
{

在ctx.tblTimeRecords
,其中(r.tblEmployee.Active&安培无功qLoggedIn =从河ClockOut.HasValue)|| System.Data.Objects.EntityFunctions.DiffDays(r.ClockOut,DateTime.Now)小于30
选择新的{名称= r.tblEmployee.Last +,+ r.tblEmployee.First,r.tblProject。 ProjName,r.ClockIn,r.ClockOut};

dataGridView1.DataSource = qLoggedIn.ToList();

}
}


解决方案

河粉请注意他们使用的WinForms不是asp.net。根据MSDN你可以做到以下几点:

 的BindingSource bindingSource1 =新的BindingSource(); 
bindingSource1.DataSource =(从ctx.tblTimeRecords
R,其中(r.tblEmployee.Active&安培;&安培;!r.ClockOut.HasValue)|| System.Data.Objects.EntityFunctions.DiffDays(R .ClockOut,DateTime.Now)小于30
选择新的{名称= r.tblEmployee.Last +,+ r.tblEmployee.First,r.tblProject.ProjName,r.ClockIn,r.ClockOut}) .ToList();

dataGridView1.DataSource = bindingSource1;



看:的 MSDN文档


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?

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 please note that they are using winforms not asp.net. According to MSDN you can do the following:

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

这篇关于数据LINQ查询绑定的DataGridView实体框架5.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆