从类对象填充Datagridview [英] Populate Datagridview from class objects
本文介绍了从类对象填充Datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在尝试填充Datagridview表单类对象
请参阅下面的示例代码
代码:
Hi All,
I am trying to populate Datagridview form class objects
Please see the sample code below
Code:
public class Customer
{
public string FirstName { get; set;}
public string LastName { get; set;}
public Address mAddress { get; set;}
}
public class Address
{
public string Street { get; set;}
public string PostalCode { get; set;}
public string City { get; set;}
}
public List<customer> SelectCustomer()
{
List<customer> CustomerList = new List<customer> ();
while(dr.Read())
{
Customer mCustomer =new Customer();
mAddress= new Address();
mCustomer.FirstName= dr["FirstName"].ToString();
mCustomer.FirstName= dr["LastName"].ToString();
mAddress.Street= dr["Street"].ToString();
mAddress. PostalCode = dr["PostalCode"].ToString();
mCustomer. mAddress= mAddress;
CustomerList.Add(mCustomer);
}
return CustomerList;
}
我使用List< customer>绑定Datagridview ,
它显示客户数据,但在地址栏中它只显示对象引用或没有数据。我已将data属性更改为mAddress。街道仍然没有显示它的价值。
将提前感谢任何帮助。
I have bind the Datagridview using List<customer> ,
It shows customer data but in the address column it only shows object reference or no data. I have changed the data property to mAddress. Street and still it does not show the value in it.
Will appreciate any help thanks in advance.
推荐答案
我认为这个小教程可能会有所帮助。
C#教程 - 将DataGridView绑定到集合 [ ^ ]
它创建一个类,汽车,并创建一个汽车列表,List< ;汽车>然后将列表绑定到DataGridView。
I think this little tutorial might be helpful.
C# Tutorial - Binding a DataGridView to a Collection[^]
It creates a class, Cars, and creates a list of cars, List<cars> and then binds the list to a DataGridView.
这篇关于从类对象填充Datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文