ADO.NET 数据绑定错误 - BindingSource.EndEdit() 更改当前位置

发布于 2024-07-15 12:29:39 字数 1071 浏览 8 评论 0原文

使用 BindingSource数据集TableAdapter? 这给我带来了永恒的困惑。

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

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

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

保存时,我调用:

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

问题是,

  • 如果我不调用 EndEdit(),则不会保存具有当前焦点的 TextBox 的
  • 更改调用 EndEdit(),BindingSource 的 Current 成员不再指向我刚刚添加的行。

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

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

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();

Upon Save, I call:

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

The problem is that

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

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?

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().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ记忆︶ㄣ 2024-07-22 12:29:39

事实证明,这是 .NET 框架的一个“功能”。 之前曾在 connect.microsoft.com 上举报过我,但该问题已被关闭为“不会修复”。 不过,有一个解决方法

我使用以下 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;
        }
    }

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.

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;
        }
    }
对岸观火 2024-07-22 12:29:39

您现在可能已经明白了这一点,但您不需要调用表适配器的 insert 方法。 只需调用 update 方法,它就会判断是否有新的或更改的记录并采取相应的操作。

You have probably figured this out by now, but you don't need to call the insert method of the table adapter. Just call the update method, and it will determine if there are any new or changed records and act accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文