Solved

Populate Datagridview - datasource list of objects each with list of objects

Posted on 2014-12-18
4
600 Views
Last Modified: 2016-02-18
HI, I need to display a list of EmpBO in a winform datagridview. Each EmpBO has a list of EmpTaskBO. Here is BO's

public class EmpBO
    {
        //BO for Employee
        private int empId;
        private string name;
      private List<EmpTaskBO> lEABO;
      
      public int EmpID
        {
            get { return empID; }
            set { empID = value; }
        }
      ...
    }

private class EmpTaskBO
    {
      private int id;
        private string taskName;

      public int ID{...}
    }

I want a DataGridView gv to display the following:

EmpID      Name      TaskName
1              Jake              DB
1              Jake              Files
2              John      Misc
2              John      Phones
3        
...

How should this be done, if the datasource is List<EmpBO> how can the child object be shown?

Thanks,
Jakob
0
Comment
Question by:edaj6
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 40508141
With a little bit of linq magic...  ;)
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q28583695
{
	public partial class Form1 : Form
	{
		private BindingSource source = new BindingSource();
		private static List<EmpBO> employees = new List<EmpBO>() 
		{ 
			new EmpBO() { EmpID = 1, Name = "Jake", Tasks = new List<EmpTaskBO>() { new EmpTaskBO() { ID = 1, TaskName = "DB" }, new EmpTaskBO() { ID = 2, TaskName = "Files" } } },
			new EmpBO() { EmpID = 2, Name = "John", Tasks = new List<EmpTaskBO>() { new EmpTaskBO() { ID = 1, TaskName = "Misc" }, new EmpTaskBO() { ID = 2, TaskName = "Phones" } } },
		};

		public Form1()
		{
			InitializeComponent();
		}

		private void OnLoad(object sender, EventArgs e)
		{
			var data = (from employee in employees
				  from task in employee.Tasks
				  select new { employee.EmpID, employee.Name, task.TaskName });
			source.DataSource = data;
			dataGridView1.DataSource = source;
		}
	}

	class EmpBO
	{
		private int empid;
		private string name;
		private List<EmpTaskBO> tasks;

		public int EmpID
		{
			get { return empid; }
			set { empid = value; }
		}

		public string Name
		{
			get { return name; }
			set { name = value; }
		}

		public List<EmpTaskBO> Tasks
		{
			get
			{
				if (tasks == null)
					tasks = new List<EmpTaskBO>();
				return tasks;
			}
			set { tasks = value; }
		}
	}

	class EmpTaskBO
	{
		private int id;
		private string taskName;

		public int ID
		{
			get { return id; }
			set { id = value; }
		}

		public string TaskName
		{
			get { return taskName; }
			set { taskName = value; }
		}
	}
}

Open in new window


Produces the following output -Capture.JPG
-saige-
0
 

Author Comment

by:edaj6
ID: 40508830
Saige, thank you very much, this is perfect.
0
 

Author Comment

by:edaj6
ID: 40508837
Oh, just noticed that objects where EmpBO.tasks is null is not showing. Is there an easy fix for this or do I have to make an outer join? (if that's allowed between parent/child objects)

Thanks again
0
 

Author Comment

by:edaj6
ID: 40508924
Ok, found the solution to last question. Im using this if anyone else interested:

from task in employee.Tasks.DefaultIfEmpty(new EmpTaskBO())
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question