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

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

问题描述

使用 BindingSource DataSet 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的当前成员不再指向我刚添加的行。

  • 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.

我可以使用表单中的值与BindingSource更新的DataTable不同,调用 Insert()这破坏了使用数据绑定的目的。我需要做些什么才能得到这个工作?

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?

我知道我可以调用 TableAdapter.Update()在整个DataSet上,因为我使用强类型的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天全站免登陆