【问题标题】:C# BindingSource.AddingNew is never called?C# BindingSource.AddingNew 从未被调用?
【发布时间】:2011-03-01 02:42:03
【问题描述】:

当我离开数据网格的单元格时,永远不会调用 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) 北 System.SecurityUtils.SecureCreateInstance(类型类型,对象 [] args) 北 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() bei DevExpress.XtraGrid.Views.Grid.GridView.OnActiveEditor_ValueModified(Object sender, EventArgs e) 北 DevExpress.XtraEditors.Repository.RepositoryItem.RaiseModified(EventArgs e) 北 DevExpress.XtraEditors.BaseEdit.OnEditValueChanging(ChangingEventArgs e) bei 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(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)

【问题讨论】:

    标签: c# datagrid bindingsource


    【解决方案1】:

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

    参考你可以看IBindingList

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

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

    【讨论】:

    • wpf... 200000 LOC 应用程序永远不会看到美丽 wpf 的光芒...msdn.microsoft.com/en-us/library/… 在那个链接上我看到了 BindingSource 属性上 AllowNew 设置为 false 的 3 个原因允许新建。我猜它的原因 3 所以我通过添加一个无参数的 Ctor 来修复它,但我仍然得到同样的错误,可能是因为有另一个 Ctor 的一个参数作为 Customer 对象的 new Guid()?
    • 我删除了第二个 Ctor,但我得到的参数仍然相同:/
    • 好的,我们开始吧:即使使用默认的 Ctor,当我分配 DataSource ICustomerList 的类型时,BindingSource 的属性窗口也将 AddNew 属性设置为 FALSE。我什至有实现 IBindingList 的 IBindingList 实现或 BindingList。尽管我满足了所有要求,但它不起作用。所以我现在捕捉到 BindingSource_AddNew 事件并通过 e.NewObject = new Customer(Guid.NewGuid()) 添加新客户;到目前为止它的工作原理......
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-03-21
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多