views:

320

answers:

2

Hello,

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.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 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)

A: 

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.

kubal5003
wpf... good one that 200000 LOC app will never see the light of beautiful wpf...http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.allownew.aspxat that link I saw the 3 reasons why AllowNew was set to falseon my BindingSource Property AllowNew.I guess its reason 3 so I fixed it by add a parameterless Ctor, butstill I get same error maybebecause there is another Ctor with one Parameter as new Guid() for the Customer object?
msfanboy
I removed the second Ctor with the parameter still same exception I get :/
msfanboy
ok here we go:even with a default Ctor the BindingSource`s property window set the AddNew property to FALSE just when I assign the type of the DataSource ICustomerList.I have even IBindingList implementent or BindingList<T> which implements IBindingList.Despite of all requirements I fullfilled it does not work.So I catch now the BindingSource_AddNew event and add new Customer viae.NewObject = new Customer(Guid.NewGuid());it works so far...
msfanboy
A: 

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.

Tom W