Binding List<T> to DataGridView in WinForm

C#ListDatagridviewBindingGrid

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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;

Solution 2 - C#

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;

Solution 3 - C#

Yes, it is possible to do with out rebinding by implementing INotifyPropertyChanged Interface.

Pretty Simple example is available here,

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

Solution 4 - C#

After adding new item to persons add:

myGrid.DataSource = null;
myGrid.DataSource = persons;

Solution 5 - C#

This isn't exactly the issue I had, but if anyone is looking to convert a BindingList of any type to List of the same type, then this is how it is done:

var list = bindingList.ToDynamicList();

Also, if you're assigning BindingLists of dynamic types to a DataGridView.DataSource, then make sure you declare it first as IBindingList so the above works.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnamcoView Question on Stackoverflow
Solution 1 - C#Jürgen SteinblockView Answer on Stackoverflow
Solution 2 - C#Dimitar DimitrovView Answer on Stackoverflow
Solution 3 - C#DevView Answer on Stackoverflow
Solution 4 - C#RafalView Answer on Stackoverflow
Solution 5 - C#KopfsView Answer on Stackoverflow