简体   繁体   中英

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. 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. Which means as a result I do have a collection of Faults inside this object. I am using the Windows Form control BindingNavigator to navigate to each machine in my system. 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. How can I get all the faults associated with machine in DataGridView. On my Machine class I have a get property that is returning all the faults given a MachineID. So, within my Machine object Faults property is returning a collection of faults like indicated on the below code. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM