0

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

DataGrid 将 BindingSource 作为数据源,BindingSource 又具有“客户”的“列表”。

BindingSource 需要什么来创建新的 Customer 对象并将其添加到底层 ICustomerList ?

当然,接口没有构造函数......

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

这就是我得到的例外:

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

bei System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfoculture, Object[] activationAttributes) bei System.SecurityUtils.SecureCreateInstance(Type type, Object[] args) bei System.ComponentModel.BindingList1.AddNewCore() bei System.ComponentModel.BindingList1.System.ComponentModel.IBindingList.AddNew() 在 System.Windows.Forms.BindingSource.AddNew() 在 System.Windows.Forms.CurrencyManager.AddNew() 在 DevExpress.Data.CurrencyDataController.OnCurrencyManagerAddNew() 在 DevExpress.Data 之后。 CurrencyDataController.AddNewRow() 在 DevExpress.XtraGrid.Views.Grid.GridView.OnActiveEditor_ValueModified(Object sender, EventArgs e) 在 DevExpress.XtraEditors.Repository.RepositoryItem.RaiseModified(EventArgs e) 在 DevExpress.XtraEditors.BaseEdit.OnEditValueChanging(ChangingEventArgs e)在 DevExpress.XtraEditors.TextEdit.OnMaskBox_ValueChanged(Object sender, EventArgs e) 在 DevExpress.XtraEditors.Mask.MaskBox.RaiseEditTextChanged() 在 System.Windows.Forms.TextBoxBase.WmReflectCommand(Message& m) 在 DevExpress.XtraEditors.Mask.MaskBox 之后。 BaseWndProc(消息&m) 北 DevExpress.XtraEditors.Mask.MaskBox.WndProc(Message& m) 北 DevExpress.XtraEditors.TextBoxMaskBox.WndProc(Message& msg) 北 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 北 System.Windows.Forms .NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

4

2 回答 2

0

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

如果您添加一个新项目,您可以在 eventargs 中将一个属性设置为“覆盖”(仅在此特定上下文中使用该词,而不是通常意义上的)正在添加的新对象,您可以在其中使用任何您喜欢的构造函数. 只需设置 e.NewObject = new YourObject。

于 2010-07-17T14:34:05.487 回答
0

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

作为参考,您可以查看IBindingList

此外,我会放弃它,因为 DataGridView 有 ICancelAddNew 的错误,如果用户在新行处于活动状态时按下 Esc 或只是离开它,那么恐怖就开始了。根据我的经验,一个更好的解决方案是有一个按钮“添加新的..”和另一个带有文本框/组合框(等等)的窗口。如果您使用的不是标准的 DataGrid 控件,那当然不是问题。

这些问题在 WPF 及其 DataGrid 组件中得到完全解决。如果这是一个新项目并且您可以切换到 WPF,我强烈建议您这样做。这意味着少了很多痛苦。

于 2010-06-08T12:05:04.390 回答