I am using a Datagridview (unbounded mode) and I have selected "enable adding" in the designer.
我正在使用Datagridview(*模式),我在设计器中选择了“启用添加”。
when the form loads, the grid is not populated (drop down to select a supplier and button to view aliases) and there is a single row displayed with the * in the tab.
加载表单时,不会填充网格(下拉选择供应商和按钮以查看别名),并在选项卡中显示一行*。
However, when I populate the grid, I no longer have this insert row for adding to the collection.
但是,当我填充网格时,我不再有这个插入行来添加到集合中。
Here's basically what I am doing:
这基本上就是我在做什么:
List<SupplierAlias> aliases = //some db lookup.
aliasGrid.AutoGenerateColumns = false;
aliasGrid.DataSource = aliases;
The columns are defined as unbound columns in the designer.
列在设计器中定义为未绑定列。
Previously, I was using clearing the aliasGrid.DataBindings but I took that out and it still removes the insert row.
以前,我使用清除aliasGrid.DataBindings,但我把它拿出来,它仍然删除插入行。
2 个解决方案
#1
Does SupplierAlias
have a public parameterless constructor? If it doesn't, you'll need to use an IBindingList
implementation to provide the new row - for example BindingSource
:
SupplierAlias是否有公共无参数构造函数?如果没有,您将需要使用IBindingList实现来提供新行 - 例如BindingSource:
BindingSource bs = new BindingSource();
bs.DataSource = yourList;
bs.AddingNew += delegate(object sender, AddingNewEventArgs args)
{
args.NewObject = new SomeType(args);
};
grid.DataSource = bs;
#2
Answering own question for others' reference.
回答自己的问题供他人参考。
I changed the List<SupplierAlias
> I used to databind to a BindingList<SupplierAlias
> and I have my insert row now.
我将List
#1
Does SupplierAlias
have a public parameterless constructor? If it doesn't, you'll need to use an IBindingList
implementation to provide the new row - for example BindingSource
:
SupplierAlias是否有公共无参数构造函数?如果没有,您将需要使用IBindingList实现来提供新行 - 例如BindingSource:
BindingSource bs = new BindingSource();
bs.DataSource = yourList;
bs.AddingNew += delegate(object sender, AddingNewEventArgs args)
{
args.NewObject = new SomeType(args);
};
grid.DataSource = bs;
#2
Answering own question for others' reference.
回答自己的问题供他人参考。
I changed the List<SupplierAlias
> I used to databind to a BindingList<SupplierAlias
> and I have my insert row now.
我将List