Binding to objects and updating read-only properties

G

Guest

I've created an app that reads data from a db and populates an BindingList
decendant with my own objects. I can then bind controls to the objects
properties and updates get saved to the database and everything is perfect.

Then I added some read-only properties to the object. Theese properties are
basically calculations done on the other properties in the object (thus they
are never stored in the database). The read-only properties bind to
read-only textboxes on my form and shows correct data when the object is
navigated to (by using the BindingNavigator)

When I update the properties of my object through the bound controls, the
read-only properties fail to update when I leave the control.

So far I’ve found three ways to update the contents of the read-only
controls, and none of them very elegant:

1. Update another property (but this leaves the values dependent on that
property with a wrong value (and I might not want to update any other values)
2. Navigate away from the record and back again. Now all values are correct
(I’ve tried a dirty fix in code to navigate to the present record, but that
failed to update the screen)
3. Save and reload the data from the database – this solution is feasible,
but will require a lot of code to make sure that I save whenever a property
change

I know there must be a solution, but I can’t find it. I’ve tried raising
several DataStatusChanged events for all dependent properties whenever a
property changed but this does not do the trick (and it would also require
that I correctly remember which properties are dependent on each other to
raise the correct number of events)
 
G

Guest

Cato Lommerud said:
I've created an app that reads data from a db and populates an BindingList
decendant with my own objects. I can then bind controls to the objects
properties and updates get saved to the database and everything is perfect.

Then I added some read-only properties to the object. Theese properties are
basically calculations done on the other properties in the object (thus they
are never stored in the database). The read-only properties bind to
read-only textboxes on my form and shows correct data when the object is
navigated to (by using the BindingNavigator)

When I update the properties of my object through the bound controls, the
read-only properties fail to update when I leave the control.

So far I’ve found three ways to update the contents of the read-only
controls, and none of them very elegant:

1. Update another property (but this leaves the values dependent on that
property with a wrong value (and I might not want to update any other values)
2. Navigate away from the record and back again. Now all values are correct
(I’ve tried a dirty fix in code to navigate to the present record, but that
failed to update the screen)
3. Save and reload the data from the database – this solution is feasible,
but will require a lot of code to make sure that I save whenever a property
change

I know there must be a solution, but I can’t find it. I’ve tried raising
several DataStatusChanged events for all dependent properties whenever a
property changed but this does not do the trick (and it would also require
that I correctly remember which properties are dependent on each other to
raise the correct number of events)
Ahh - some soul-searching detected that raising the DataStatusChanged after
actually modifying the property does indeed produce the correct behaviour.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top