【问题标题】:wpf DataGrid set itemsource List<object>wpf DataGrid set itemsource List<object>
【发布时间】:2018-12-09 06:18:52
【问题描述】:

我在 wpf 中遇到了 DataGrid 的问题

这是一个类:

class Superviser
    {
        public long Id = 0;
        public string name = "";
        public string father = "";
        public string code = "";
    }

这是一个构建此类对象列表的函数

public List<Superviser> allSuperviser()
        {
            return db.tbPersons.Where(i => i.level == StaticsObject.isSuperviser).Select(x => new Superviser
            {
                Id = x.Id,
                name = x.firstName,
                father = x.father,
                code = x.code,
            }).ToList();
        }

我使用此代码在 datagrid 中设置此列表

dgvPerson.ItemsSource = classPerson.allSuperviser();

但是运行程序时datagrid是空的!

提示:列表不为空。

问题出在哪里?

如何在 DataGrid 上显示此列表?

【问题讨论】:

    标签: list class object datagrid itemsource


    【解决方案1】:

    你好,我解决了它

    我把班级改成:

    class Superviser
    {
        public long Id { get; set; }
        public string name { get; set; }
        public string father { get; set; }
        public string code { get; set; }
        public Superviser() { }
    
        public Superviser(long Id, string name, string father, string code)
        {
            this.Id = Id;
            this.name = name;
            this.father = father;
            this.code = code;
        }
    }
    

    并将函数更改为:

    public List<Superviser> allSuperviser()
            {
                return db.tbPersons.Where(i => i.level == StaticsObject.isSuperviser).Select(x => new Superviser { Id = x.Id, name = x.firstName + " " + x.lastName, father = x.father, code = x.code }).ToList();
            }
    

    问题已解决:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      • 2011-10-10
      相关资源
      最近更新 更多