C# Как отобразить список объектов, каждый из которых имеет другой объект в качестве свойства, в представлении datagridvi ⇐ C#
-
Anonymous
C# Как отобразить список объектов, каждый из которых имеет другой объект в качестве свойства, в представлении datagridvi
So I currently have two objects.
Object A
public class Person { public string FirstName { get; set; } public string LastName { get; set; } public Pet MyPet { get; set; } } and Object B
public class Pet { public string Name { get; set; } public string Animal { get; set; } } So then I created 2 Person objects and 2 Pet objects.
Pet myDog = new Pet("Fluffy", "Dog"); Person A = new Person("John", "Smith", myDog); Pet myCat = new Pet("Furball", "Cat"); Person B = new Person("Jane", "Doe", myCat); Then I added Person A and Person B into a List of Person objects, and made the list the data source for my data grid.
The results were not what wanted. The data grid auto populated the columns as FirstName, LastName, and MyPet, instead of also including Name and Animal. Also, while the FirstName and LastName columns properly displayed the names of both people, MyPet showed TestDemo.Pet, neither object name, Name, or Animal.
So I did some digging and used the designer to add in the columns themselves and making their DataPropertyName corresponding to how they were named in the class. While they did fill up correctly for the Person object (I even switched the positions of their first names and last name and it works), the columns for the pets did not work.
So I am wondering why is that the case?
As an aside, is there also a way to make the columns sort-able?
Источник: https://stackoverflow.com/questions/781 ... -object-as
So I currently have two objects.
Object A
public class Person { public string FirstName { get; set; } public string LastName { get; set; } public Pet MyPet { get; set; } } and Object B
public class Pet { public string Name { get; set; } public string Animal { get; set; } } So then I created 2 Person objects and 2 Pet objects.
Pet myDog = new Pet("Fluffy", "Dog"); Person A = new Person("John", "Smith", myDog); Pet myCat = new Pet("Furball", "Cat"); Person B = new Person("Jane", "Doe", myCat); Then I added Person A and Person B into a List of Person objects, and made the list the data source for my data grid.
The results were not what wanted. The data grid auto populated the columns as FirstName, LastName, and MyPet, instead of also including Name and Animal. Also, while the FirstName and LastName columns properly displayed the names of both people, MyPet showed TestDemo.Pet, neither object name, Name, or Animal.
So I did some digging and used the designer to add in the columns themselves and making their DataPropertyName corresponding to how they were named in the class. While they did fill up correctly for the Person object (I even switched the positions of their first names and last name and it works), the columns for the pets did not work.
So I am wondering why is that the case?
As an aside, is there also a way to make the columns sort-able?
Источник: https://stackoverflow.com/questions/781 ... -object-as
Мобильная версия