views:

3106

answers:

2

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?

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

+2  A: 

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;
        }
    }
cdonner
A: 

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.

Carla Hepker