How to display List<string> property in DataGridView in Windows Form

277 Views Asked by At

so I am having a problem and I can't find the right answer. So I have the Department class:

 public class Department
{
    // attributes
    public int DNumber { get; set; }
    public string DName { get; set; }
    public List<string> Locations { get; set; }
    // relationships
    public List<Employee> Employees { get; set; }
    public Employee Manager { get; set; }
    public List<Project> Projects { get; set; }
    // one-to-many relationship (manager) attribute
    public string MgrStartDate { get; set; }

}

this is how I add value:

public static void CreateDepartment(IObjectContainer db, string fileName)
    {
        if (File.Exists(fileName))
        {
            FileStream fs = new FileStream(fileName, FileMode.Open);
            StreamReader fin = new StreamReader(fs);
            int nDpms = int.Parse(fin.ReadLine());
            for (int i = 0; i < nDpms; i++)
            {
                string line = fin.ReadLine();
                if (line != null)
                {
                    string[] fields = line.Split(':');
                    int dnumber = int.Parse(fields[0]);
                    string dname = fields[1];
                    string[] locationFields = fields[2].Split(',');
                    List<string> locations = new List<string>(locationFields);

                    Department d = new Department
                    {
                        DNumber = dnumber,
                        DName = dname,
                        Locations = locations
                    };
                    db.Store(d);
                }
            }
            fin.Close();
            fs.Close();
        }
    }

and I want to display List<'string> Locations (don't mind the 'string). So I use this code:

private void Form_Department_Load(object sender, EventArgs e)
    {
        Database.DbFileName = "Database.yap";
        db = Database.Db;

        loadDepartment();
    }

    private void loadDepartment()
    {
        var department = new Department();
        result = db.QueryByExample(department);
        dataGridView_Department.DataSource = result;
    }

But the DataGridView only show things that are not List<'T>

enter image description here

As you can see, it doesn't have Locations, or Employees,...

That is my question. Thank you

0

There are 0 best solutions below