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()
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()
bei DevExpress.Data.CurrencyDataController.AddNewRow()
bei DevExpress.XtraGrid.Views.Grid.GridView.OnActiveEditor_ValueModified(Object sender, EventArgs e)
bei DevExpress.XtraEditors.Repository.RepositoryItem.RaiseModified(Eve开发者_StackOverflow社区ntArgs 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)
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.
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.
精彩评论