1、两个GRID绑定父子关系的数据源绑定。如果先新增子数据源记录,则会出现异常:System.MissingMethodException: Constructor on type
'System.ComponentModel.IBindingList' not found.
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr,
Binder binder, Object[] args, CultureInfo culture, Object[]
activationAttributes)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr,
Binder binder, Object[] args, CultureInfo culture, Object[]
activationAttributes)
这是因为父对象还未创建记录。
解决方法:
1、在子数据源新增对象时判断:
if (((System.ComponentModel.IBindingList)this.childBindingSource.List).AllowNew )
{
this.childBindingSource.AddNew();
}
2、父数据源的ListChanged事件中,确保主数据源在无数据时,子数据源无法先新增。
private void leftBindingSource_ListChanged(object sender, ListChangedEventArgs e)
{
Console.WriteLine("leftBindingSource_ListChanged");
if (leftBindingSource.Current == null)
this.edgRight.AllowUserToAddRows = false;
else
this.edgRight.AllowUserToAddRows = true;
}