0

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
2
  • According to your design An Employee can have more than one department public List<Departments> AssociatedDepartment {get;set;} Commented Apr 7, 2015 at 11:54
  • Yes an Employee can have more than one Department. Commented Apr 7, 2015 at 12:00

2 Answers 2

1

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Add the following property in Employee :

public string Department
{
    get
    {
        string departments = string.Empty;
        foreach (Department department in AssociatedDepartment)
        {
            if (departments.Length != 0)
                departments += ", ";
            departments += department.DepartmentName;
        }
        return departments;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.