不能让用户添加行到DataGridView中使用列表&LT;&GT;数据源 [英] Can't Allow User To Add Rows to DataGridView with List<> Datasource
问题描述
大家好,的
我有的DataGridView
是绑定到名单,其中,MyClass的&GT;
为数据源
。
但是,当我设置 AllowUserToAddRows
属性设置为真有什么出现。
I've DataGridView
that bound to List<myClass>
as DataSource
.
But when i set "AllowUserToAddRows
" property to "True" there is nothing appear.
我试图改变数据源
到的BindingList&LT; MyClass的&GT;
,它的顺利
I tried to change the DataSource
to BindingList<myClass>
and it's going well.
我不知道是否应该取代我的名单,其中;&GT;
与的BindingList&LT;&GT;
或有为更好的解决方案?
I wonder if i should replace my List<>
with BindingList<>
or there is better solution ??.
推荐答案
确实 MyClass的
有一个公共的无参数的构造函数?如果没有,你可能源自的BindingList&LT; T&GT;
并覆盖 AddNewCore
来调用自定义构造函数
Does myClass
have a public parameterless constructor? If not, you could derive from BindingList<T>
and override AddNewCore
to call your custom constructor.
(编辑),或者 - 只是包装你的列表中的的BindingSource
,它可以工作:
(edit) Alternatively - just wrap your list in a BindingSource
and it may work:
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 } } });
}
}
这篇关于不能让用户添加行到DataGridView中使用列表&LT;&GT;数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!