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
EndEdit()
, the changes of the TextBox with the current focus are not savedEndEdit()
, 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().
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;
}
}