I have a data object bound to a ComboBox, and when the selected value in that ComboBox is changed I'd like to update the data object and then refresh the datagrid the object is bound to.
The issue I'm encountering is that as far as I can tell, the event handler I've attached fires before the data object is updated and so when my data grid refreshes it still contains the old ComboBox value.
Here is my ComboBox control initialisation:
cmbPaymentType.DataSource = _paymentTypes
cmbPaymentType.DisplayMember = "PaymentTypeName"
cmbPaymentType.DataBindings.Add("SelectedItem", _data, "PaymentType", False, DataSourceUpdateMode.OnPropertyChanged)
cmbPaymentType.Refresh()
AddHandler cmbPaymentType.DropDownClosed, AddressOf _data.NotifyDataChanged
When NotifyDataChanged is triggered, the data grid which uses my data object _data
is refreshed, however the value of PaymentType is not updated. If I trigger NotifyDataChanged afterwards from elsewhere the data grid updates to the selected combo box value which indicates that the data object binding works, it's just being bound too late in my application flow.
How can I trigger an event after the data binding is updated?
Thanks!
Edit: the data object _data
is an auto-generated Entity Framework class with a partial class I've written attached to it. If I were to use the INotifyPropertyChanged
interface I do not know how to override the automatically generated properties to implement it correctly.
@Fabio's solution below solved my problem by manually updating the data binding when the combo box selection changed, meaning my event handling could take place afterwards and the data object would be updated before the data grid refreshed. I've rejigged his function slightly to work with the first data binding on any control:
Private Sub bindingUpdate(sender As Object, e As EventArgs)
DirectCast(sender, Control).DataBindings(0).WriteValue()
_data.NotifyDataChanged(sender, e)
End Sub