none
Databinding two grids - parent child from entity framework RRS feed

  • Question

  • I cannot find any examples of binding a parent and child grid where the datasource is entity objects. I need the two grids to be in sync - when parent record is selected in parent grid, child records should display in child grid. Here is my attempt:

    Employees = parent entity

    EmployeeForms = child entity

    var results = from ncrecord in admincontext.Employees.Include("EmployeeForms")
                              where ncrecord.EmployeeForms.Any(p => p.DueDate < DateTime.Now)
                              select ncrecord;

    results.Load();

    grdParent.DataSource = bindingSourceEmployees;
    grdChild.DataSource = bindingSourceForms;

     bindingSourceEmployees.DataSource = dbcontext.Employees.Local.ToBindingList(); // have also tried just dbcontext

    bindingSourceEmployees.DataMember = ??; // "Employees" does not work - DataMember property 'Employees' cannot be found on the DataSource.

     bindingSourceForms.DataSource = bindingSourceEmployees;

    bindingSourceForms.DataMember = ??; // have tried name of relation

    I can't use the entity name for datamember, or the data relationship. Both cause errors.

    • Moved by Barry Wang Friday, February 21, 2014 2:25 AM
    Thursday, February 20, 2014 9:00 PM

Answers

  • Hello,

    If I understand correctly, you want to display master and child table data in DataGridView with Entity Framework, and want to have a function: parent record is selected in parent grid, child records should display in child grid. Have a try below example:

    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel;
    
    using System.Data;
    
    using System.Drawing;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    using System.Windows.Forms;
    
    
    namespace P20140220_Databindingtwogrids
    
    {
    
        public partial class Form1 : Form
    
        {
    
            protected List<Order> OrderList = new List<Order>();
    
    
            protected List<OrderDetail> OrderDetailList = new List<OrderDetail>();
    
    
            public Form1()
    
            {
    
                InitializeComponent();
    
    
                using (WinFormDBEntities db = new WinFormDBEntities())
    
                {
    
                    var result = from order in db.Orders
    
                                 select order;
    
    
                    OrderList = result.ToList();
    
    
                    foreach (Order o in OrderList)
    
                    {
    
                        foreach (OrderDetail OD in o.OrderDetails)
    
                        {
    
                            OrderDetailList.Add(OD);
    
                        }
    
                    }
    
    
    
                }
    
            }
    
    
            private void button1_Click(object sender, EventArgs e)
    
            {
    
                this.dataGridView1.DataSource = OrderList.Select(o => new { o.OrderID, o.OrderCode, o.OrderName }).ToList();
    
            }
    
    
            private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
    
            {
    
                if (e.Row.Index >= 0)
    
                {
    
                    int OrderID = (int)e.Row.Cells["OrderID"].Value;
    
    
                    this.dataGridView2.DataSource = OrderDetailList.Where(o => o.OrderID == OrderID).Select(o => new { o.OrderDetailID, o.OrderDetailCode, o.OrderDetailName, o.DataTime, o.OrderID }).ToList();
    
                }
    
            }
    
    
    
        }
    
    }
    

    The result:

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by RLuther Tuesday, February 25, 2014 3:59 PM
    Friday, February 21, 2014 6:13 AM
    Moderator

All replies

  • Hi RLuther,

    Since your issue seems related to Entity Framework, I just have a look at this blog:

    http://msdn.microsoft.com/en-us/data/jj682076.aspx

    It seems this issue may more appropriate for you to post on

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    So I'll help you move it.

    Regards,



    Barry Wang
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 21, 2014 2:24 AM
  • Hello,

    If I understand correctly, you want to display master and child table data in DataGridView with Entity Framework, and want to have a function: parent record is selected in parent grid, child records should display in child grid. Have a try below example:

    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel;
    
    using System.Data;
    
    using System.Drawing;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    using System.Windows.Forms;
    
    
    namespace P20140220_Databindingtwogrids
    
    {
    
        public partial class Form1 : Form
    
        {
    
            protected List<Order> OrderList = new List<Order>();
    
    
            protected List<OrderDetail> OrderDetailList = new List<OrderDetail>();
    
    
            public Form1()
    
            {
    
                InitializeComponent();
    
    
                using (WinFormDBEntities db = new WinFormDBEntities())
    
                {
    
                    var result = from order in db.Orders
    
                                 select order;
    
    
                    OrderList = result.ToList();
    
    
                    foreach (Order o in OrderList)
    
                    {
    
                        foreach (OrderDetail OD in o.OrderDetails)
    
                        {
    
                            OrderDetailList.Add(OD);
    
                        }
    
                    }
    
    
    
                }
    
            }
    
    
            private void button1_Click(object sender, EventArgs e)
    
            {
    
                this.dataGridView1.DataSource = OrderList.Select(o => new { o.OrderID, o.OrderCode, o.OrderName }).ToList();
    
            }
    
    
            private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
    
            {
    
                if (e.Row.Index >= 0)
    
                {
    
                    int OrderID = (int)e.Row.Cells["OrderID"].Value;
    
    
                    this.dataGridView2.DataSource = OrderDetailList.Where(o => o.OrderID == OrderID).Select(o => new { o.OrderDetailID, o.OrderDetailCode, o.OrderDetailName, o.DataTime, o.OrderID }).ToList();
    
                }
    
            }
    
    
    
        }
    
    }
    

    The result:

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by RLuther Tuesday, February 25, 2014 3:59 PM
    Friday, February 21, 2014 6:13 AM
    Moderator
  • Well, this does work. thank you.
    Tuesday, February 25, 2014 3:59 PM