Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

790 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