且构网

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

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

更新时间:2022-03-22 03:00:55

List 没有实现 IBindingList 所以网格不知道你的新项目.

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

将您的 DataGridView 绑定到 BindingList.

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;