[英]Binding a collection inside a data source object to a DataGridView in C#
I do have two sql server database tables, which are Machine and Fault. 我确实有两个sql server数据库表,分别是Machine和Fault。 Machine table have all the information regarding a specific machine here on my site.
机器表在我的网站上提供了有关特定机器的所有信息。 Fault table stores all mechanical faults associated with a specific machine, that is, one machine can have many faults (one to many relationship).
故障表存储与一台特定机器相关的所有机械故障,即一台机器可以有许多故障(一对多关系)。 I do have a data source that is binded to the Machine object.
我确实有一个绑定到Machine对象的数据源。 Which means as a result I do have a collection of Faults inside this object.
这意味着结果是我确实在该对象内部有一系列Faults。 I am using the Windows Form control BindingNavigator to navigate to each machine in my system.
我正在使用Windows窗体控件BindingNavigator导航到系统中的每台计算机。 I am able to view each machine information.
我能够查看每台机器的信息。 However, I am struggling to view all the faults that is associated with the selected machine in DataGridView.
但是,我正在努力查看与DataGridView中选定计算机相关的所有故障。 How can I get all the faults associated with machine in DataGridView.
如何在DataGridView中获取与计算机相关的所有故障。 On my Machine class I have a get property that is returning all the faults given a MachineID.
在我的Machine类上,我有一个get属性,该属性返回给定MachineID的所有错误。 So, within my Machine object Faults property is returning a collection of faults like indicated on the below code.
因此,在我的Machine对象的Faults属性中,将返回错误的集合,如下面的代码所示。 Please assist?
请协助?
public override ICollection<Fault> Faults
{
get
{
//returning all faults associated with a given machine
using (var context = new AllEntities())
{
var faultsList = (from f in context.Faults
where f.MachineID == MachineID
select f).ToList<Fault>();
return faultsList; //return the list of faults
}
}
The trick is to bind the master grid via BindingSource
and then bind the detail grid DataSource property to the same binding source. 技巧是通过
BindingSource
绑定主网格,然后将详细信息网格的DataSource属性绑定到相同的绑定源。
Here is a full working demo: 这是一个完整的工作演示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Samples
{
public class Master
{
public string Name { get; set; }
public ICollection<Detail> Details { get; set; }
}
public class Detail
{
public string Name { get; set; }
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new Form();
var split = new SplitContainer { Dock = DockStyle.Fill, Parent = form, Orientation = Orientation.Horizontal };
var dgvMaster = new DataGridView { Dock = DockStyle.Fill, Parent = split.Panel1 };
var dgvDetail = new DataGridView { Dock = DockStyle.Fill, Parent = split.Panel2 };
var data = Enumerable.Range(1, 10).Select(p => new Master
{
Name = "P" + p,
Details = Enumerable.Range(1, 10).Select(c => new Detail
{
Name = "C" + p + "." + c,
}).ToList()
}).ToList();
var bsMaster = new BindingSource { DataSource = data };
dgvMaster.DataSource = bsMaster;
dgvDetail.DataBindings.Add("DataSource", bsMaster, "Details", true, DataSourceUpdateMode.Never);
Application.Run(form);
}
}
}
The essential part is: 基本部分是:
var bsMaster = new BindingSource { DataSource = data };
dgvMaster.DataSource = bsMaster;
dgvDetail.DataBindings.Add("DataSource", bsMaster, "Details", true, DataSourceUpdateMode.Never);
Replace Master
with Machine
, Detail
with Fault
, "Details" with "Faults` and the result will be your model. 更换
Master
用Machine
, Detail
与Fault
,“详细信息”与“Faults`,其结果将是您的模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.