Powiązanie listy z DataGridView w WinForm
Mam klasę
class Person{
public string Name {get; set;}
public string Surname {get; set;}
}
I List<Person>
do których dodaję kilka pozycji. Lista jest związana z moim 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;
Nie ma żadnego problemu. myGrid
wyświetla dwa wiersze, ale kiedy dodaję nowe pozycje do mojej listy persons
, myGrid
nie wyświetla nowej zaktualizowanej listy. Pokazuje tylko dwa wiersze, które dodałem wcześniej.
Więc w czym problem?
Rebinding za każdym razem działa dobrze. Ale kiedy przywiązuję DataTable
do siatki, kiedy za każdym razem, gdy wprowadzam jakieś zmiany do DataTable
, nie ma żadnych potrzeba ReBind myGrid
.
Jak go rozwiązać, nie zmieniając za każdym razem?
70
Author: Michele Ceo, 2013-05-22
4 answers
Lista nie implementuje IBindingList
, więc siatka nie wie o nowych elementach.
Zamiast tego Połącz DataGridView z BindingList<T>
.
var list = new BindingList<Person>(persons);
myGrid.DataSource = list;
Ale poszedłbym nawet dalej i przywiązał Twoją siatkę do 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/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-01 05:23:31
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-01 05:23:31
Za każdym razem, gdy dodajesz nowy element do listy, musisz ponownie powiązać siatkę. Coś w stylu:
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/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-22 15:34:18
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-22 15:34:18
Po dodaniu nowej pozycji do persons
dodaj:
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/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-22 15:36:26
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-22 15:36:26
Tak, można to zrobić z out rebindingiem poprzez implementację interfejsu INotifyPropertyChanged.
Dość prosty przykład jest dostępny tutaj,
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/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-22 16:36:49
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-22 16:36:49