您的位置:首页 > 其它

数据绑定常见问题

2009-07-09 15:15 357 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: