C#如何使用存储在List <object>的对象内的对象的属性填充datagridview

时间:2017-07-06 20:13:59

标签: c# list object datagridview populate

  public abstract class Person
    {
        public string Name { get; set; }
        public int Age { get; set; } 
    }

    public class Manager:Person
    {
        public int Trophies { get; set; }

    }
    public class Team
    {
        public string Name { get; set; }
        public Manager manager { get; set; }
        public Team() { }

        public Team(string _name,Manager _mananger)
        {
            Name = _name;
            manager = _mananger;
        Lists.ListOfTeams.Add(this);
        }
    }
    public static class Lists {
        public static List<Team> ListOfTeams = new List<Team>();
    }


My code : 
datagridTeams.Datasource = Lists.ListOfTeams;

我想用一个团队列表填充一个数据网格,也就是他们的经理名称,但是我无法实现这一点,它只是给我结果ListOfTeams.Manger; 任何人都可以帮助我如何实现这一目标

1 个答案:

答案 0 :(得分:0)

您可以公开在Team类中获取经理姓名(ManagerName)的公共获取属性。这将帮助您实现您的目标。

public class Team
{
    public string Name { get; set; }
    public string ManagerName => Manager.Name;
    public Manager manager { get; set; }
    public Team() { }

    public Team(string _name,Manager _mananger)
    {
        Name = _name;
        manager = _mananger;
    Lists.ListOfTeams.Add(this);
    }
}