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

一曲冷凌霜 提交于 2019-12-08 02:04:37

问题


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


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/686597/ado-net-databinding-bug-bindingsource-endedit-changes-current-position

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