Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding List<T> to DataGridView in WinForm

I have a class

class Person{       public string Name {get; set;}       public string Surname {get; set;} } 

and a List<Person> to which I add some items. The list is bound to my DataGridView.

List<Person> persons = new List<Person>(); persons.Add(new Person(){Name="Joe", Surname="Black"}); persons.Add(new Person(){Name="Misha", Surname="Kozlov"}); myGrid.DataSource = persons; 

There is no problem. myGrid displays two rows, but when I add new items to my persons list, myGrid does not show new updated list. It only shows the two rows which I added before.

So what is the problem?

Rebinding every time works well. But when I bind a DataTable to the grid when every time when I make some changes to DataTable there is not any need to ReBind myGrid.

How to solve it without rebinding every time?

like image 713
namco Avatar asked May 22 '13 15:05

namco


People also ask

What is DataGridView data binding?

The DataGridView control supports the standard Windows Forms data binding model, so it can bind to a variety of data sources. Usually, you bind to a BindingSource that manages the interaction with the data source.

How do you bind data to the grid in Windows application?

In this blog I am going to describe how to bind the data source with “DataGridView” when window load. Create a Window form application have a look at my previous blog - how to create window form application. Once our window is ready click on Toolbox under “Data” choose “DataGridView” and drag onto the window.

What is binding list in C#?

BindingList is a generic list type that has additional binding support. While you can bind to a generic list, BindingList provides additional control over list items, i.e. if they can be edited, removed or added. BindingList also surfaces events that notify when the list has been changed.


2 Answers

List does not implement IBindingList so the grid does not know about your new items.

Bind your DataGridView to a BindingList<T> instead.

var list = new BindingList<Person>(persons); myGrid.DataSource = list; 

But I would even go further and bind your grid to a BindingSource

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; 
like image 191
Jürgen Steinblock Avatar answered Oct 01 '22 09:10

Jürgen Steinblock


Every time you add a new element to the List you need to re-bind your Grid. Something like:

List<Person> persons = new List<Person>(); persons.Add(new Person() { Name = "Joe", Surname = "Black" }); persons.Add(new Person() { Name = "Misha", Surname = "Kozlov" }); dataGridView1.DataSource = persons;  // added a new item persons.Add(new Person() { Name = "John", Surname = "Doe" }); // bind to the updated source dataGridView1.DataSource = persons; 
like image 45
Dimitar Dimitrov Avatar answered Oct 01 '22 09:10

Dimitar Dimitrov