C# BindingSource.AddingNew 从未被调用?
当我离开数据网格的单元格时,永远不会调用 BindingSource.AddingNew。
DataGrid 将 BindingSource 作为数据源,它又具有“Customer”的“List”。
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, CultureInfo Culture, Object[] activateAttributes) bei System.SecurityUtils.SecureCreateInstance(类型类型,Object[] args) 位于 System.ComponentModel.BindingList1.AddNewCore() 位于 System.ComponentModel.BindingList
1.System.ComponentModel.IBindingList.AddNew() 位于 System.Windows.Forms.BindingSource.AddNew() 位于 System.Windows.Forms.CurrencyManager.AddNew() 位于 DevExpress.Data.CurrencyDataController.OnCurrencyManagerAddNew() 位于 DevExpress.Data.CurrencyDataController.AddNewRow() bei DevExpress.XtraGrid.Views.Grid.GridView.OnActiveEditor_ValueModified(对象发送者,EventArgs e) 是 DevExpress.XtraEditors.Repository.RepositoryItem.RaiseModified(EventArgs e) 位于 DevExpress.XtraEditors.BaseEdit.OnEditValueChanging(ChangingEventArgs e) bei DevExpress.XtraEditors.TextEdit.OnMaskBox_ValueChanged(对象发送者,EventArgs e) 位于 DevExpress.XtraEditors.Mask.MaskBox.RaiseEditTextChanged() bei System.Windows.Forms.TextBoxBase.WmReflectCommand(Message& m) 北 DevExpress.XtraEditors.Mask.MaskBox.BaseWndProc(Message&m) 北 DevExpress.XtraEditors.Mask.MaskBox.WndProc(Message&m) 北 DevExpress.XtraEditors.TextBoxMaskBox.WndProc(Message& msg) 是 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) bei System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)
BindingSource.AddingNew is never called when I leave the cell of my datagrid.
The DataGrid has as datasource the BindingSource which again has a "List" of "Customer".
What does the BindingSource need to create a new Customer object and add it to the underlying ICustomerList ?
Of course a interface has no constructor...
but my customer object has a default constructor!
Thats the Exception I get:
System.MissingMethodException: The constcructor for the type "SAT.EnCoDe.Administration.ICustomer" was not found.
bei System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
bei System.SecurityUtils.SecureCreateInstance(Type type, Object[] args)
bei System.ComponentModel.BindingList1.AddNewCore()
1.System.ComponentModel.IBindingList.AddNew()
bei System.ComponentModel.BindingList
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 sender, EventArgs e)
bei DevExpress.XtraEditors.Repository.RepositoryItem.RaiseModified(EventArgs e)
bei DevExpress.XtraEditors.BaseEdit.OnEditValueChanging(ChangingEventArgs e)
bei DevExpress.XtraEditors.TextEdit.OnMaskBox_ValueChanged(Object sender, EventArgs e)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要使用 AddNew,则用于数据绑定的对象需要具有无参数构造函数。显然接口没有构造函数,所以这是相当痛苦的。您也不能将抽象类用于此目的,因为它无法实例化。唯一的方法是使用具体类型作为层次结构的根。
作为参考,您可以查看 IBindingList
此外,我会放弃它,因为 DataGridView 在 ICancelAddNew 方面存在错误,如果用户在新行处于活动状态时按 Esc 或只是离开它,那么恐怖就开始了。根据我的经验,更好的解决方案是有一个按钮“添加新..”和另一个带有文本框/组合框(等等)的窗口。如果您使用标准 DataGrid 控件以外的其他 DataGrid 控件,那么这当然不是问题。
这些问题在 WPF 及其 DataGrid 组件中得到了彻底解决。如果这是一个新项目并且您可以切换到 WPF,我强烈建议您这样做。这意味着痛苦少了很多。
The object that is meant for databinding needs to have parameterless constructor if AddNew is to be used. Obviously interfaces don't have constructors so this is quite a pain. You can't also use abstract class for this purpose because it can't be instantiated. The only way is to use a concrete type as a root of your hierarchy.
For reference you can look at IBindingList
Besides I would give it up because DataGridView has bugs with ICancelAddNew and if a user presses Esc when new row is active or simply leaves it then the horror starts. From my experience a better solution is to have a button "Add new.." and another windows with textboxes/comboboxes(and so on). That's of course not an issue if you're using some other DataGrid control than the standard one.
Those problems are completely resolved in WPF and its DataGrid component. If it's a new project and you can switch to WPF I would strongly suggest it. It means a lot less pain.
我不确定我是否理解了你的问题;当您离开单元格时,为什么您的绑定源会添加新项目?
如果您添加一个新项目,您可以在 eventargs 中设置一个属性来添加新的,该属性“覆盖”(仅在特定上下文中使用该词,而不是通常意义上的)要添加的新对象,其中您可以使用任何您喜欢的构造函数。只需设置 e.NewObject = new YourObject。
I'm not sure I've understood your question; why would your bindingsource add a new item when you leave a cell?
IF you add a new item, you can set a property in the eventargs to AddingNew that 'overrides' (using the word only in this specific context and not in the usual sense) the new object being added wherein you can use any constructor you please. Simply set e.NewObject = new YourObject.