Q:

DataGridView Add Multiple Rows for List in List Object

I have two classes Employee and Department as follows
class Employee
{
    public string ID {get;set;}
    public string Name {get;set;}
    public List<Departments> AssociatedDepartment {get;set;}
}

class Department
{
   public string DepartmentID {get;set;}
   public string DepartmentName {get;set;}
}

I am binding a DataGridView with List<Employee>. It is showing list of Employees but not Department of the employees.
List<Employee> employeeList = GetEmployeeList();

if (employeeList != null)
{
     grdResponse.DataSource = employeeList;
}

Now it is showing following data.
ID  Name  
---------
1   Joe  
2   Tim

What should I do to get following?
ID  Name Department 
--------------------
1   Joe  Software
1   Joe  Management
2   Tim  Hardware

A:

you can extend Employee class with get-only property (the column will be ReadOnly)
class Employee
{
    public string ID { get; set; }
    public string Name { get; set; }

    public string Departments
    {
        get 
        { 
           if (AssociatedDepartment == null || AssociatedDepartment.Count == 0)
              return string.Empty;
           return string.Join(", ", AssociatedDepartment.Select(d => d.DepartmentName));
        }
    }

    public List<Department> AssociatedDepartment { get; set; }
}

