Binding BindingList
In this WinForm scenario, a class representing a person and a List
Understanding the Binding
The problem arises from the fact that List does not implement IBindingList. Consequently, the DataGridView is unaware of any changes made to the list.
Solution: BindingList
To resolve this issue, consider binding the DataGridView to a BindingList
var list = new BindingList<Person>(persons); myGrid.DataSource = list;
BindingSource for Even More Control
For even greater control over data binding, consider using a BindingSource as an intermediary between the BindingList
var list = new List<Person>() { new Person { Name = "Joe", }, new Person { Name = "Misha", }, }; var bindingList = new BindingList<Person>(list); var source = new BindingSource(bindingList, null); grid.DataSource = source;
By employing this approach, you can maintain a consistent and dynamic binding between your data source and the DataGridView, ensuring that updates to the underlying data are reflected in real-time.
The above is the detailed content of Why Doesn't My WinForms DataGridView Update After Adding Items to a List?. For more information, please follow other related articles on the PHP Chinese website!