2010-06-08 117 views
0

当我离开我的数据网格的单元格时,从不会调用BindingSource.AddingNew。C#BindingSource.AddingNew永远不会被调用?

DataGrid具有BindingSource的数据源,该BindingSource又具有“Customer”的“List”。

BindingSource需要创建一个新的Customer对象并将其添加到底层的ICustomerList中?

当然的接口没有构造...

,但我的客户对象有一个默认的构造函数!

那异常我得到:

System.MissingMethodException: The constcructor for the type "SAT.EnCoDe.Administration.ICustomer" was not found. 

贝System.RuntimeType.CreateInstanceImpl(的BindingFlags bindingAttr,粘结剂粘结剂,对象[]指定参数时,CultureInfo的文化,对象[] activationAttributes) 贝System.SecurityUtils.SecureCreateInstance (Type type,Object [] args) bei System.ComponentModel.BindingList 1.AddNewCore() bei System.ComponentModel.BindingList 1.System.ComponentModel.IBindingList.AddNew() bei System.Windows.Forms.BindingSource.AddNew() bei System.Windows.Forms.CurrencyManager .AddNew() bei DevExpress.Data.CurrencyDataController。 OnCurrencyManagerAddNew() 贝DevExpress.Data.CurrencyDataController.AddNewRow() 贝DevExpress.XtraGrid.Views.Grid.GridView.OnActiveEditor_ValueModified(对象发件人,EventArgs的) 贝DevExpress.XtraEditors.Repository.RepositoryItem.RaiseModified(EventArgs的) 贝DevExpress.XtraEditors.BaseEdit.OnEditValueChanging(ChangingEventArgs E) 贝DevExpress.XtraEditors.TextEdit.OnMaskBox_ValueChanged(对象发件人,EventArgs的) 贝DevExpress.XtraEditors.Mask.MaskBox.RaiseEditTextChanged() 贝System.Windows.Forms.TextBoxBase .WmReflectCommand(Message & m) be DevExpress.XtraEditors.Mask.MaskBox.BaseWndProc(Message & m) be DevExpress.XtraEditors.Ma sk.MaskBox.WndProc(消息&米) 贝DevExpress.XtraEditors.TextBoxMaskBox.WndProc(消息& MSG) 贝System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&米) 贝System.Windows.Forms的。 NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

回答

0

如果要使用AddNew,则用于数据绑定的对象需要具有无参数构造函数。显然接口没有构造函数,所以这是一个很痛苦的事情。你不能也为此使用抽象类,因为它不能被实例化。唯一的方法是使用具体类型作为层次结构的根。

仅供参考,你可以看看IBindingList

而且我会放弃它,因为DataGridView中有ICancelAddNew漏洞,如果用户按下Esc键时,新的行激活或者干脆离开它那么恐怖开始。根据我的经验,更好的解决方案是有一个“添加新的”按钮和另一个带有文本框/组合框的窗口(依此类推)。如果您使用的是其他DataGrid控件而不是标准控件,那当然不是问题。

这些问题在WPF及其DataGrid组件中完全解决。如果这是一个新项目,你可以切换到WPF,我会强烈建议。这意味着更少的痛苦。

+0

wpf ...好的一个,200000 LOC应用程序永远不会看到美丽的WPF光... http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource。 allownew.aspx 在那个链接我看到了AllowNew被设置为false的3个原因,在我的BindingSource属性AllowNew上被设置为false 。 我想其原因3,所以我固定它通过添加一个参数的构造函数,但 我仍然得到同样的错误,也许 因为有另一个构造函数,带一个参数为新的GUID()为客户对象? – msfanboy 2010-06-08 12:22:03

+0

我删除了第二个Ctor,但参数仍然是我得到的异常:/ – msfanboy 2010-06-08 12:29:48

+0

确定这里我们去: 即使使用默认Ctor BindingSource的属性窗口将AddNew属性设置为FALSE只是当我分配DataSource的类型ICustomerList。 我甚至有IBindingList实现或BindingList 它实现IBindingList。 尽管所有的要求,我fullfilled它不起作用。 因此,我现在赶上BindingSource_AddNew事件并通过 添加新客户e.NewObject = new Customer(Guid.NewGuid()); 它迄今为止工作... – msfanboy 2010-06-08 18:28:29

0

我不确定我了解你的问题;为什么当你离开一个单元格时,你的绑定资源会添加一个新的项目?

如果你添加一个新项目,你可以在eventargs中设置一个属性为AddingNew'覆盖'(仅在这个特定的上下文中使用这个词,而不是通常意义上)正在添加的新对象,你可以使用任何你想要的构造函数。只需设置e.NewObject = new YourObject。

相关问题