koorosh rad
koorosh rad

Reputation: 63

wpf DataGrid set itemsource List<object>

I have a problem with DataGrid in wpf

this is a class :

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

and this is a function that build a list of this class object

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();
        }

and I use this code to set this list in datagrid

dgvPerson.ItemsSource = classPerson.allSuperviser();

but when run program datagrid is empty !

tip : The list is not empty.

Where is the problem?

How do I display this list on DataGrid?

Upvotes: 1

Views: 66

Answers (1)

koorosh rad
koorosh rad

Reputation: 63

hello agian i solved it

I changed class to :

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;
    }
}

and change function to :

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();
        }

And the problem was fixed :)

Upvotes: 1

Related Questions