How to Bind a List
Binding a List
Consider the following scenario:
class Person{ public string Name {get; set;} public string Surname {get; set;} }
You may have a List
List<Person> persons = new List<Person>(); persons.Add(new Person(){Name="Joe", Surname="Black"}); persons.Add(new Person(){Name="Misha", Surname="Kozlov"});
Initially, these items will be displayed in the DataGridView. However, if you add new items to the persons list, the DataGridView won't show them.
The Issue
The problem lies in that List doesn't implement IBindingList, which means the DataGridView doesn't receive notifications about changes in the data.
Solution
To resolve this, use a BindingList
var list = new BindingList<Person>(persons); myGrid.DataSource = list;
Advanced Binding with BindingSource
You can take this further by using a BindingSource, which provides additional functionality like sorting, filtering, and concurrency.
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 following these techniques, you can bind a List
The above is the detailed content of How to Dynamically Update a WinForms DataGridView Bound to a List?. For more information, please follow other related articles on the PHP Chinese website!