我有一个 DataGridView
与 DataSource
设置为 List<myClass>
但是,当我设置 AllowUserToAddRows
时,新行指示器不显示至 true
,
当我设置 DataSource
至 BindingList<myClass>
,这似乎解决了问题。
问:应该替换我的 List<>
与 BindingList<>
或者有更好的解决方案?
最佳答案
是否 myClass
有一个公共(public)的无参数构造函数?如果不是,您可以从 BindingList<T>
派生并覆盖 AddNewCore
调用您的自定义构造函数。
(edit) 或者 - 只需将您的列表包装在 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<Person> { new Person { Name = "Fred" } };
BindingSource bs = new BindingSource();
bs.DataSource = people;
Application.Run(new Form { Controls = { new DataGridView {
Dock = DockStyle.Fill, DataSource = bs } } });
}
}
关于c# - AllowUserToAddRows 不适用于 DataGridView 上的 List<> 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1565875/