ADO.NET 数据绑定错误 - BindingSource.EndEdit() 更改当前位置 [英] ADO.NET databinding bug - BindingSource.EndEdit() changes current position

查看:18
本文介绍了ADO.NET 数据绑定错误 - BindingSource.EndEdit() 更改当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 BindingSource, 数据集TableAdapter?这让我永远感到困惑.

What is the correct order of processing an insert from a data-bound control using BindingSource, DataSet, and TableAdapter? This is causing me eternal confusion.

我有一个用于添加新行的表单.

I have a form that is used to add a new row.

在显示表格之前,我调用:

Before showing the form, I call:

bindingSource.AddNew();
bindingSource.MoveLast();

保存后,我调用:

bindingSource.EndEdit();
tableAdapter.Insert([the row given to me as bindingSource.Current]);

问题是

  • 如果我不调用EndEdit(),当前焦点的TextBox的变化不会被保存
  • 如果我调用 EndEdit(),BindingSource 的 Current 成员将不再指向我刚刚添加的行.
  • if I don't call EndEdit(), the changes of the TextBox with the current focus are not saved
  • if I do call EndEdit(), the BindingSource's Current member no longer points to the row that I just added.

我当然可以使用表单中的值调用 Insert(),而不是由 BindingSource 更新的 DataTable,但这违背了使用数据绑定的目的.我需要做什么才能使其正常工作?

I can of course call Insert() with the values from the form as opposed to the DataTable that was updated by the BindingSource, but that defeats the purpose of using data binding. What do I need to do in order to get this working?

我知道我可以在整个 DataSet 上调用 TableAdapter.Update(),因为我使用的是强类型 DataSet.不过,我在表中有未绑定数据的外键,并且我在调用 Insert() 之前添加了这些外键.

I understand that I could call TableAdapter.Update() on the entire DataSet, since I am using a strongly typed DataSet. I have foreign keys in the table that are not datab-bound, though, and that I am adding in before I call Insert().

推荐答案

原来这是 .NET 框架.我之前曾在 connect.microsoft.com 上被报道过,但问题被关闭为不会解决".不过,有一个解决方法.

It turns out that this is a 'feature' of the .NET framework. I has been previously reported on connect.microsoft.com, but the issue was closed as "will not fix". There is a workaround, though.

我正在使用以下 C# 代码来重置 ListChanged 事件处理程序中的位置:

I am using the following C# code to reset the position in a ListChanged event handler:

    [...]
        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;
        }
    }

这篇关于ADO.NET 数据绑定错误 - BindingSource.EndEdit() 更改当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆