无法允许用户使用List Datasource将行添加到DataGridView

我将DataGridView绑定到List作为DataSource 。 但是当我将“ AllowUserToAddRows ”属性设置为“True”时,没有任何内容出现。

我试图将DataSource更改为BindingList ,并且进展顺利。

我想知道我是否应该用BindingList替换我的List BindingList或者有更好的解决方案。

myClass是否有一个公共无参数构造函数? 如果没有,您可以从BindingList派生并覆盖AddNewCore以调用您的自定义构造函数。

(编辑)或者 – 只需将您的列表包装在BindingSource ,它可能会起作用:

 using System; using System.Windows.Forms; using System.Collections.Generic; public class Person { public string Name { get; set; } [STAThread] static void Main() { var people = new List { new Person { Name = "Fred" } }; BindingSource bs = new BindingSource(); bs.DataSource = people; Application.Run(new Form { Controls = { new DataGridView { Dock = DockStyle.Fill, DataSource = bs } } }); } }