ADO.NET databinding bug - BindingSource.EndEdit() changes current position

十年热恋 提交于 2019-12-06 06:13:48
cdonner

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;
        }
    }

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!