Context
I recently wrote a final exam that required using a BindingSource object with ADO.NET to populate a number of combo box and label controls from which the user could perform basic business functions. I got it working but had some reservations about my design choice.
The setup
When a user selected a product from the combo box, the binding updated two labels of the cost and quantity-on-hand of the product. I then decimal-parsed the cost from the label's Text property to pass on to a purchase form.
The problem
I initially coded a SelectedIndexChanged event for the combo box control and then invoked a method to calculate a purchase total. However, I realized that it was taking the cost label's Text value before the binding updated the value, leaving an index lag (i.e. the calculated cost was for the previously-selected product).
My fix was to invoke the calculate method upon the cost label's TextChanged event instead of the combo box's SelectedIndexChanged event. It worked, but felt I compromised a design principal somewhere.
My question
What would be a better way to do this? I can see several possible avenues:
- Can the binding source be called to immediately update associated fields before the calculate method is invoked?
- Should I decant the cost value directly from the binding source using a parsed DataRowView ToString instead of sourcing it from a UI feature?
- Is there a better way of getting values from a binding source other than the previous way that I don't know about?
- Should I just dispense with the binding source and work with the DataTable object directly?
Appreciate any insight. Thanks!