且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

绑定列表< T>到WinForm中的DataGridView

更新时间:2022-03-22 03:01:07

不实现 IBindingList ,所以网格不了解您的新项目。

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

将您的DataGridView绑定到 BindingList< T>

Bind your DataGridView to a BindingList<T> instead.

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

但我甚至会进一步将您的网格绑定到一个 BindingSource

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;