Link LINK request to datagridView binding in Entity Framework 5.0

I am learning Entity Framework (5.0 and VSExpress 2012), and I am having real problems binding my request to dataGridView in WinForms. I have the code below and it shows my query in the order when I run the application, but I do not know what I need to do to update the dataGridView after changing the data in the base database. What is the best way to do 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(); } } 
+6
source share
4 answers

Please note that they use 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

+7
source

You need to bind data using dataGridView1.DataBind(); :

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

.Net uses a disabled model. When you retrieve information from a database, this is a snapshot at that point in time. If you change the data in the underlying data warehouse, these changes will not be reflected unless you explicitly re-query the database and reinstall your user interface.

When you save the changes in the user interface, EF can check if someone else has changed the changed line (for concurrency) and will tell you if there is a potential conflict.

0
source
  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; 
0
source

Source: https://habr.com/ru/post/925792/


All Articles