Solved

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

Posted on 2014-12-18
4
570 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
  • 3
4 Comments
 
LVL 33

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

Technology Partners: 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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

735 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