win form下的DataGridView邦定了某种IEnumerable<T>的数据源之后怎样保持动态更新?
如下,有这段程序
在 button被点击的时候我会给List<mm> 增加一条记录,可是List<mm>是被邦定到dataGridView上的,按理说dataGridView应该实时更新增加的内容,可是dataGridView还是保持以前的老样子,什么都不更新,除非我先把它 = null,然后再assign value,可是这样做我觉得是非常愚蠢的办法,有没有什么方法能让dataGridView自动更新内容呢?
另外,dataGridView的Update(), Refersh()我都试过,没有用的,请高手指教
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ml.Add(new mm("opp", "mmn"));
ml.Add(new mm("opp1", "mmn1"));
dataGridView1.DataSource = ml;
}
private void button1_Click(object sender, EventArgs e)
{
ml.Add(new mm("sdfdsf", "qwwqe"));
//dataGridView1.DataSource = null; 除非先把它 = null,否则dataGridView不更新内容
dataGridView1.DataSource = ml;
}
List<mm> ml = new List<mm>();
}
public class mm
{
public string Name { get; set; }
public string Age { get; set; }
public mm(string n, string a)
{
Name = n;
Age = a;
}
}
------解决方案--------------------
List <mm> ml = new List <mm>();
public Form1()
{
InitializeComponent();
ml.Add(new mm("opp", "mmn"));
ml.Add(new mm("opp1", "mmn1"));
dataGridView1.DataSource = ml;
}
private void button1_Click(object sender, EventArgs e)
{
ml.Add(new mm("sdfdsf", "qwwqe"));
dataGridView1.DataSource = ml;
}
还不行就用用BindingList<T>对象代替Ilist<T>
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = ml ;
dataGridView1.DataSource = bindingSource;