2010-06-08 116 views
0

當我離開我的數據網格的單元格時,從不會調用BindingSource.AddingNew。C#BindingSource.AddingNew永遠不會被調用?

DataGrid具有BindingSource的數據源,該BindingSource又具有「Customer」的「List」。

BindingSource需要創建一個新的Customer對象並將其添加到底層的ICustomerList中?

當然的接口沒有構造...

,但我的客戶對象有一個默認的構造函數!

那異常我得到:

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

貝System.RuntimeType.CreateInstanceImpl(的BindingFlags bindingAttr,粘結劑粘結劑,對象[]指定參數時,CultureInfo的文化,對象[] activationAttributes) 貝System.SecurityUtils.SecureCreateInstance (Type type,Object [] args) bei System.ComponentModel.BindingList 1.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() 貝DevExpress.Data.CurrencyDataController.AddNewRow() 貝DevExpress.XtraGrid.Views.Grid.GridView.OnActiveEditor_ValueModified(對象發件人,EventArgs的) 貝DevExpress.XtraEditors.Repository.RepositoryItem.RaiseModified(EventArgs的) 貝DevExpress.XtraEditors.BaseEdit.OnEditValueChanging(ChangingEventArgs E) 貝DevExpress.XtraEditors.TextEdit.OnMaskBox_ValueChanged(對象發件人,EventArgs的) 貝DevExpress.XtraEditors.Mask.MaskBox.RaiseEditTextChanged() 貝System.Windows.Forms.TextBoxBase .WmReflectCommand(Message & m) be DevExpress.XtraEditors.Mask.MaskBox.BaseWndProc(Message & m) be DevExpress.XtraEditors.Ma sk.MaskBox.WndProc(消息&米) 貝DevExpress.XtraEditors.TextBoxMaskBox.WndProc(消息& MSG) 貝System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&米) 貝System.Windows.Forms的。 NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

回答

0

如果要使用AddNew,則用於數據綁定的對象需要具有無參數構造函數。顯然接口沒有構造函數,所以這是一個很痛苦的事情。你不能也爲此使用抽象類,因爲它不能被實例化。唯一的方法是使用具體類型作爲層次結構的根。

僅供參考,你可以看看IBindingList

而且我會放棄它,因爲DataGridView中有ICancelAddNew漏洞,如果用戶按下Esc鍵時,新的行激活或者乾脆離開它那麼恐怖開始。根據我的經驗,更好的解決方案是有一個「添加新的」按鈕和另一個帶有文本框/組合框的窗口(依此類推)。如果您使用的是其他DataGrid控件而不是標準控件,那當然不是問題。

這些問題在WPF及其DataGrid組件中完全解決。如果這是一個新項目,你可以切換到WPF,我會強烈建議。這意味着更少的痛苦。

+0

wpf ...好的一個,200000 LOC應用程序永遠不會看到美麗的WPF光... http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource。 allownew.aspx 在那個鏈接我看到了AllowNew被設置爲false的3個原因,在我的BindingSource屬性AllowNew上被設置爲false 。 我想其原因3,所以我固定它通過添加一個參數的構造函數,但 我仍然得到同樣的錯誤,也許 因爲有另一個構造函數,帶一個參數爲新的GUID()爲客戶對象? – msfanboy 2010-06-08 12:22:03

+0

我刪除了第二個Ctor,但參數仍然是我得到的異常:/ – msfanboy 2010-06-08 12:29:48

+0

確定這裏我們去: 即使使用默認Ctor BindingSource的屬性窗口將AddNew屬性設置爲FALSE只是當我分配DataSource的類型ICustomerList。 我甚至有IBindingList實現或BindingList 它實現IBindingList。 儘管所有的要求,我fullfilled它不起作用。 因此,我現在趕上BindingSource_AddNew事件並通過 添加新客戶e.NewObject = new Customer(Guid.NewGuid()); 它迄今爲止工作... – msfanboy 2010-06-08 18:28:29

0

我不確定我瞭解你的問題;爲什麼當你離開一個單元格時,你的綁定資源會添加一個新的項目?

如果你添加一個新項目,你可以在eventargs中設置一個屬性爲AddingNew'覆蓋'(僅在這個特定的上下文中使用這個詞,而不是通常意義上)正在添加的新對象,你可以使用任何你想要的構造函數。只需設置e.NewObject = new YourObject。

相關問題