public class Customer
{
public int id { get; set; }
public Person CustPerson { get; set; }
public Customer()
{
this.CustPerson = new Person();
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
List<Customer> CustomerList=new List<Customer>();
for (int i = 0; i <= 4; i++)
{
Customer cust = new Customer();
cust.id = i;
cust.CustPerson.FirstName = "john" + i.ToString();
cust.CustPerson.LastName = "deo" + i.ToString();
cust.CustPerson.Age = 20 + i;
CustomerList.Add(cust);
}
dataGridView1.DataSource = CustomerList;
}
我想firstname
的屬性一類的屬性,lastname
& age
在gridview
被打印出來,但我得到一個列,而不是三個,其中包含mynamespace.person
: - (存取含有其他類
幫我出家夥...任何幫助表示讚賞
人們常常說,我們應該更喜歡組成了繼承,但在這個(經典)的情況,因爲一個顧客可能*是一個人,或許你應該改變你的方法,讓'顧客'從'Person'派生,而不是擁有'Person'屬性。 – phoog