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

时间:2014-05-29 16:52:08

标签: c# linq entity-framework datagridview

有人可以举例说明如何将结果绑定到实体框架中从多个表(来自连接查询)获取的datagridview。实际上我可以将数据绑定到datagridview但是当我调用

context.SaveChanges(); 数据库中没有更新任何内容。如何使用Update,Insert,Delete函数将数据正确绑定到datagridview。

这就是我的尝试。

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";

提前致谢。

1 个答案:

答案 0 :(得分:0)

在给出查询结果之后,你应该写下这个:

...

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

...