1.DataGridView数据绑定
1 namespace WindowsFormsApplication1 2 { 3 public partial class Form1 : Form 4 { 5 private List<Student> _students = new List<Student>(); 6 7 public Form1() 8 { 9 InitializeComponent(); 10 11 _students.Add(new Student() { Name = "黄勇", Age = 25, School = "哈尔滨商业大学" }); 12 13 _students.Add(new Student() { Name = "何明", Age = 26, School = "湖南理工大学" }); 14 15 _students.Add(new Student() { Name = "Tommy", Age = 32, School = "香港大学" }); 16 19 var bindingList = new BindingList<Student>(_students); 20 23 dataGridView1.DataSource = bindingList; 24 25 } 26 27 private void button1_Click(object sender, EventArgs e) 28 { 29 var student = _students.FirstOrDefault(item => item.Name.Equals("何明")); 30 31 if (student != null) 32 { 33 //可动态刷新UI 34 student.Name = "28"; 35 } 36 } 37 } 38 39 public class Student : INotifyPropertyChanged 40 { 41 public event PropertyChangedEventHandler PropertyChanged; 42 43 private String _name; 44 private Int32 _age; 45 private String _school; 46 47 public String Name 48 { 49 get 50 { 51 return _name; 52 } 53 set 54 { 55 if (value != _name) 56 { 57 _name = value; 58 this.NotifyPropertyChanged("Name"); 59 } 60 } 61 } 62 63 public Int32 Age 64 { 65 get 66 { 67 return _age; 68 } 69 set 70 { 71 if (value != _age) 72 { 73 _age = value; 74 this.NotifyPropertyChanged("Age"); 75 } 76 } 77 } 78 79 public String School 80 { 81 get 82 { 83 return _school; 84 } 85 set 86 { 87 if (value != _school) 88 { 89 _school = value; 90 this.NotifyPropertyChanged("School"); 91 } 92 } 93 } 94 95 private void NotifyPropertyChanged(string name) 96 { 97 if (PropertyChanged != null) 98 { 99 PropertyChanged(this, new PropertyChangedEventArgs(name)); 100 } 101 } 102 } 103 }