Winforms ADO.NET数据绑定错误-BindingSource.EndEdit()更改当前位置
使用和从数据绑定控件处理插入的正确顺序是什么?这使我永远感到困惑 我有一个用于添加新行的表单 在显示表格之前,我呼吁:Winforms ADO.NET数据绑定错误-BindingSource.EndEdit()更改当前位置,winforms,data-binding,dataset,bindingsource,tableadapter,Winforms,Data Binding,Dataset,Bindingsource,Tableadapter,使用和从数据绑定控件处理插入的正确顺序是什么?这使我永远感到困惑 我有一个用于添加新行的表单 在显示表格之前,我呼吁: bindingSource.AddNew(); bindingSource.MoveLast(); 保存后,我呼吁: bindingSource.EndEdit(); tableAdapter.Insert([the row given to me as bindingSource.Current]); 问题是 如果我不调用EndEdit(),则不会保存对当前焦点的文本框
bindingSource.AddNew();
bindingSource.MoveLast();
保存后,我呼吁:
bindingSource.EndEdit();
tableAdapter.Insert([the row given to me as bindingSource.Current]);
问题是
- 如果我不调用
,则不会保存对当前焦点的文本框所做的更改EndEdit()
- 如果我调用
,BindingSource的当前成员将不再指向我刚才添加的行EndEdit()
Insert()
,而不是使用BindingSource更新的DataTable,但这会破坏使用数据绑定的目的。我需要做什么才能让它工作
我知道我可以对整个数据集调用
TableAdapter.Update()
,因为我使用的是强类型数据集。但是,表中有未绑定数据的外键,我在调用Insert()之前添加了这些外键。事实证明,这是框架的一个“功能”。我以前曾被报道过,但该问题因“无法解决”而被关闭。不过,还有一个问题
我使用以下C代码重置ListChanged事件处理程序中的位置:
[...]
bindingSource.ListChanged +=
new ListChangedEventHandler(PreserveCurrentPosition);
[...]
private void PreserveCurrentPosition(object sender, ListChangedEventArgs e)
{
if (e.ListChangedType == System.ComponentModel.ListChangedType.ItemAdded &&
((BindingSource)sender).Count - e.NewIndex > 1)
{
((BindingSource)sender).Position = e.NewIndex;
}
}
您现在可能已经解决了这个问题,但不需要调用表适配器的insert方法。只需调用update方法,它将确定是否有任何新的或更改的记录,并相应地执行操作