BindingList<T>与gridcontrol绑定

首先自定义类,比如Person

 1  class Person {
 2         string firstName;
 3         string secondName;
 4         string comments;
 5         public Person(string firstName, string secondName) {
 6             this.firstName = firstName;
 7             this.secondName = secondName;
 8             comments = String.Empty;
 9             this.sex = false;
10         }
11         public Person(string firstName, string secondName,bool sex,List<Pet> pets)
12         {
13             this.firstName = firstName;
14             this.secondName = secondName;
15             this.sex = sex;
16             comments = String.Empty;
17             this.pets = pets; ;
18         }
19         public Person(string firstName, string secondName, string comments)
20             : this(firstName, secondName) {
21             this.comments = comments;
22         }
23         public string FirstName {
24             get { return firstName; }
25             set { firstName = value; }
26         }
27         public string SecondName {
28             get { return secondName; }
29             set { secondName = value; }
30         }
31         public string Comments {
32             get { return comments; }
33             set { comments = value; }
34         }
35         private bool sex;
36         public bool Sex
37         {
38             get { return sex; }
39             set { sex = value; }
40         }
41         private List<Pet> pets;
42 
43         public List<Pet> Pets
44         {
45             get { return pets; }
46             set { pets = value; }
47         }
 1   BindingList<Person> gridDataList = new BindingList<Person>();
 2         void InitGrid()
 3         {
 4             List<Pet> pets=new List<Pet>(){new Pet("laifu","dog")};
 5             gridDataList.Add(new Person("John", "Smith",true,pets));
 6             gridDataList.Add(new Person("Gabriel", "Smith"));
 7             gridDataList.Add(new Person("Ashley", "Smith", "some comment"));
 8             gridDataList.Add(new Person("Adrian", "Smith", "some comment"));
 9             gridDataList.Add(new Person("Gabriella", "Smith", "some comment"));
10             gridControl.DataSource = gridDataList;
11         }

最后呈现的效果:

 

转载于:https://www.cnblogs.com/wangkejie/p/7737374.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值