When adding a new item via a BindingNavigator
, how can I populate several fields of the new item with values from the previously selected item?
Note: On BindingNavigatorAddNewItem.Click
, the BindingNavigator
already has the new item loaded and the controls cleared to default values.
The BindingNavigator
has to be bound to a data source. For it to share the same as a DataGridView
, for instance, it needs to use a BindingSource
. So the added item is actually added to your underlying BindingSource.DataSource
.
There are two ways I can see to solve your issue.
Once both the DataGridView
and BindingNavigator
are bound, even though you click on the BindingNavigator.AddNewItem
button, a call to BindingSource.AddNew()
method is made and the position of the CurrencyManager
updates changes for your newly added item's position, so does the BindingSource.Current
property by returning an instance of your newly added object, or else, the BindingSource.CurrencyManager.Current
does the same.
1 - Use the
BindingSource.Current
Type cast the BindingSource.Current
object, assign your values, and you're done!
2 - Subscribe to the
BindingSource.AddingNew
event
By doing so, you may access your instance through the BindingSource.AddignNewEventArgs
, and use the AddignNewEventArgs.NewObject
property to type cast it and assign your own default values to it.