1.DataGridView数据绑定

 namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private List<Student> _students = new List<Student>(); public Form1()
{
InitializeComponent(); _students.Add(new Student() { Name = "黄勇", Age = , School = "哈尔滨商业大学" }); _students.Add(new Student() { Name = "何明", Age = , School = "湖南理工大学" }); _students.Add(new Student() { Name = "Tommy", Age = , School = "香港大学" }); var bindingList = new BindingList<Student>(_students); dataGridView1.DataSource = bindingList; } private void button1_Click(object sender, EventArgs e)
{
var student = _students.FirstOrDefault(item => item.Name.Equals("何明")); if (student != null)
{
//可动态刷新UI
student.Name = "";
}
}
} public class Student : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; private String _name;
private Int32 _age;
private String _school; public String Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
this.NotifyPropertyChanged("Name");
}
}
} public Int32 Age
{
get
{
return _age;
}
set
{
if (value != _age)
{
_age = value;
this.NotifyPropertyChanged("Age");
}
}
} public String School
{
get
{
return _school;
}
set
{
if (value != _school)
{
_school = value;
this.NotifyPropertyChanged("School");
}
}
} private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
}
05-12 21:50