简体   繁体   English

如何使用Entity Framework将数据从多个表绑定到datagridview并使用CRUD操作?

[英]How to bind data from mutiple tables to datagridview using Entity Framework and use CRUD operations?

Can some one please give a example of how to bind result to a datagridview which taken from multiple tables (from a join query) in entity framework. 有人可以举例说明如何将结果绑定到实体框架中从多个表(来自连接查询)获取的datagridview。 actually i can bind data to the datagridview but when i call 实际上,我可以将数据绑定到datagridview但是当我调用时

context.SaveChanges(); context.SaveChanges(); nothing has updated in the database. 数据库中没有更新任何内容。 How to correctly bind data to datagridview with Update,Insert,Delete functions. 如何使用Update,Insert,Delete函数将数据正确绑定到datagridview。

This is what i tried. 这是我试过的。

public class DataBindingProjection
{
    public string dono { get; set; }
    public int apmntid { get; set; }
    public string servicedesc { get; set; }
    public string cusid { get; set; }
    public string empid { get; set; 
    public bool isdelivered { get; set; }
}


context = new HHCSEntities();

var query = from d in context.DeliveryOrders
            join a in context.Appointments on d.ApmntId equals a.ApmntId
            join s in context.ServiceCategories on d.ServiceId equals s.ServiceId
            join e in context.Employees on d.EmpId equals e.EmpId
            select new DataBindingProjection
            {
                dono = d.DONo,
                apmntid = a.ApmntId,
                servicedesc = s.ServiceDesc,
                cusid = a.CusId,
                empid = d.EmpId,
                shortname = e.ShrtName,
                isdelivered = d.IsDelivered
            };

dataGridView1.DataSource = query.ToList();
dataGridView1.Columns[1].DataPropertyName = "dono";
dataGridView1.Columns[2].DataPropertyName = "apmntid";
dataGridView1.Columns[3].DataPropertyName = "servicedesc";
dataGridView1.Columns[4].DataPropertyName = "apmntid";
dataGridView1.Columns[5].DataPropertyName = "empid";
dataGridView1.Columns[9].DataPropertyName = "isdelivered";

Thanks in advance. 提前致谢。

After to give result of your query, you should write this: 在给出查询结果之后,你应该这样写:

...

BindingSource bi = new BindingSource();
bi.DataSource = query.ToList();
dataGridView1.DataSource = bi;
dataGridView1.Refresh();

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用实体框架C#将两个表绑定到Datagridview - How to bind two tables to Datagridview using entity framework C# 使用实体框架从两个表中绑定数据网格视图 - Bind Data Grid View from two tables using Entity framework 如何将数据从Web服务绑定到ASP.NET中的DevExpress网格视图,以及如何对数据使用CRUD操作? - How to bind data from webservice to DevExpress gridview in ASP.NET and to use CRUD operations on the data? 将数据从Entity Framework模型绑定到DataGridView C# - Bind data from Entity Framework model to DataGridView C# 将数据绑定到 Datagridview 的特定列 - 实体框架 - Bind Data to Specific Columns of Datagridview - Entity Framework 如何将DataGridView与两个表绑定,并在WPF中执行CRUD操作 - How to bind DataGridView with two tables, and perform CRUD operation in WPF 如何使用实体框架在一个dataGridView中合并多个表? - How to combine multiple tables in one dataGridView using Entity Framework? 将WS中的数据绑定到“ DevEx ASP.NET GridView”并在数据上使用CRUD操作? - bind data from WS to “DevEx ASP.NET GridView” and to use CRUD operations on the data? 使用实体框架为CRUD操作实现通用存储库时出错 - Error on implementing generic Repository for CRUD operations using Entity Framework 存储库模式使用实体框架,代码优先和CRUD操作 - Repository Pattern using Entity Framework, code first and CRUD operations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM