If you change the list to a BindingList, you will have success. I put together a sample that just had a DataGridView and a couple of buttons on the form.
Button
button1 generates some fake data and assigns a data source. button2 adds another Client to the main list.
The DataGridView is updated when the underlying BindingList changes.
code:
public class Customer { public int Id { get; set; } public string Name { get; set; } public string SurName { get; set; } } public partial class Form1 : Form { BindingList<Customer> customers = new BindingList<Customer>(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < 10; ++i) { customers.Add(new Customer { Id = i, Name = "Name" + i, SurName = "Surname" + i }); } dataGridView1.DataSource = customers; } private void button2_Click(object sender, EventArgs e) { customers.Add(new Customer { Id = 22, Name = "Newname", SurName = "Newsurname" }); } }
Now the reason is that BindingList<T> implements IBindingList , by the way, and this interface has, among other things, an event called ListChanged that occurs when a list or something in the list changes.
source share