Data binding linq query to datagridView in Entity Framework 5.0

谁说我不能喝 提交于 2019-12-03 14:58:04
Lance

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

You have to bind the data with dataGridView1.DataBind();:

...
dataGridView1.DataSource = qLoggedIn.ToList();
dataGridView1.DataBind();
...

.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.

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.

 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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!