当我离开数据网格的单元格时,BindingSource.AddingNew永远不会被调用。
DataGrid将BindingSource作为数据源,该数据源同样具有“客户”的“列表”。
要创建新的Customer对象并将其添加到底层ICustomerList中,BindingSource需要做些什么?
当然,接口没有构造函数...
但是我的customer对象有一个默认的构造函数!
这是我得到的异常:
System.MissingMethodException: The constcructor for the type "SAT.EnCoDe.Administration.ICustomer" was not found.bei System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr,粘合剂,Object[]参数,CultureInfo培养,Object[] activationAttributes) bei BE型,bei System.ComponentModel.BindingList1.AddNewCore() bei System.ComponentModel.BindingList1.System.ComponentModel.IBindingList.AddNew() bei Object[] () bei DevExpress.Data.CurrencyDataController.AddNewRow() bei System.Windows.Forms.CurrencyManager.AddNew() bei DevExpress.Data.CurrencyDataController.OnCurrencyManagerAddNew() bei System.Windows.Forms.BindingSource.AddNew() bei System.ComponentModel.BindingList1.AddNewCore() bei System.ComponentModel.BindingList1.System.ComponentModel.IBindingList.AddNew()bei System.Windows.Forms.BindingSource.AddNew()bei System.ComponentModel.BindingList1.AddNewCore() bei System.ComponentModel.BindingList1.System.ComponentModel.IBindingList.AddNew()bei System.Windows.Forms.BindingSource.AddNew()bei System.Windows.Forms.CurrencyManager.AddNew()bei DevExpress.Data.CurrencyDataController.OnCurrencyManagerAddNew()bei DevExpress.Data.CurrencyDataController.AddNewRow()bei DevExpress.XtraGrid.Views.Grid.GridView.OnActiveEditor_ValueModified(Object发送者,bei ) bei DevExpress.XtraEditors.Repository.RepositoryItem.RaiseModified(EventArgs e) bei DevExpress.XtraEditors.BaseEdit.OnEditValueChanging(ChangingEventArgs e) bei DevExpress.XtraEditors.TextEdit.OnMaskBox_ValueChanged(Object EventArgs,bei ) bei DevExpress.XtraEditors.Mask.MaskBox.RaiseEditTextChanged() bei System.Windows.Forms.TextBoxBase.WmReflectCommand(Message& m) bei DevExpress.XtraEditors.Mask.MaskBox.BaseWndProc(Message& m) bei DevExpress.XtraEditors.Mask.MaskBox.WndProc(Message& m) bei DevExpress.XtraEditors.TextBoxMaskBox.WndProc(Message& msg) bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) bei System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)
发布于 2010-06-08 20:05:04
如果要使用AddNew,则用于数据绑定的对象需要有无参数构造函数。显然,接口没有构造函数,所以这是一件很痛苦的事情。您也不能使用抽象类来实现此目的,因为它不能被实例化。唯一的方法是使用一个具体的类型作为层次结构的根。
作为参考,您可以查看IBindingList
此外,我会放弃它,因为DataGridView的ICancelAddNew有bug,如果用户在新行处于活动状态时按Esc键,或者干脆离开它,那么可怕的事情就开始了。根据我的经验,一个更好的解决方案是有一个按钮“添加新的.”和另一个带有文本框/组合框的窗口(等等)。如果您使用的不是标准的DataGrid控件,那么这当然不是问题。
这些问题在WPF和它的DataGrid组件中完全解决了。如果这是一个新项目,并且您可以切换到WPF,我强烈建议您这样做。这意味着更少的痛苦。
发布于 2010-07-17 22:34:05
我不确定我是否理解了您的问题;为什么您的bindingsource会在您离开单元格时添加一个新项?
如果你添加了一个新的项目,你可以在eventargs中设置一个属性AddingNew来“覆盖”正在添加的新对象(只在这个特定的上下文中使用这个词,而不是通常意义上的),其中你可以使用任何你喜欢的构造函数。只需设置e.NewObject =新YourObject即可。
https://stackoverflow.com/questions/2996650
复制相似问题