无法允许用户使用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 } } }); } }