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 }
最后呈现的效果: