Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-12-18
4
Medium Priority
?
729 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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 …
Introduction to Processes

722 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