Solved

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

Posted on 2014-12-18
4
490 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 32

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now