Enlazar lista a DataGridView en WinForm


Tengo una clase

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

Y a List<Person> a la que añado algunos elementos. La lista está vinculada a mi 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;

No hay problema. myGrid muestra dos filas, pero cuando agrego nuevos elementos a mi lista persons, myGrid no muestra una nueva lista actualizada. Solo muestra las dos filas que agregué antes.

Entonces, ¿cuál es el problema?

Rebinding cada vez funciona bien. Pero cuando ato un DataTable a la cuadrícula cuando cada vez que hago algunos cambios a DataTable no hay ninguna necesita volver a enlazar myGrid.

¿Cómo resolverlo sin volver a enlazar cada vez?

Author: Michele Ceo, 2013-05-22

4 answers

List no implementa IBindingList por lo que la cuadrícula no sabe acerca de sus nuevos elementos.

Enlaza tu DataGridView a un BindingList<T> en su lugar.

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

Pero incluso iría más lejos y enlazaría tu cuadrícula a un 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;
 137
Author: Jürgen Steinblock,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-01 05:23:31

Cada vez que añadas un nuevo elemento a la Lista necesitas volver a enlazar tu Cuadrícula. Algo como:

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;
 3
Author: Dimitar Dimitrov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-22 15:34:18

Después de añadir un nuevo elemento a persons añadir:

myGrid.DataSource = null;
myGrid.DataSource = persons;
 1
Author: Rafal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-22 15:36:26

Sí, es posible hacer con out rebinding implementando la interfaz INotifyPropertyChanged.

Un ejemplo bastante simple está disponible aquí,

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

 1
Author: Dev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-22 16:36:49