gpt4 book ai didi

c# - 数据绑定(bind) linq 查询到 Entity Framework 5.0 中的 datagridView

转载 作者:太空狗 更新时间:2023-10-29 21:23:36 25 4
gpt4 key购买 nike

我正在学习 Entity Framework (5.0 和 VSExpress 2012),但在将我的查询绑定(bind)到 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, DateTime.Now)<30
select new { Name = r.tblEmployee.Last + ", " + r.tblEmployee.First, r.tblProject.ProjName, r.ClockIn, r.ClockOut }).ToList();

dataGridView1.DataSource = bindingSource1;

参见:msdn documentation

关于c# - 数据绑定(bind) linq 查询到 Entity Framework 5.0 中的 datagridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12501183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com