Home All Groups Group Topic Archive Search About

Binding to objects and updating read-only properties

Author
24 Apr 2006 12:03 PM
Cato Lommerud
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)

Author
24 Apr 2006 12:46 PM
Cato Lommerud
Show quote
"Cato Lommerud" wrote:

> 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.

AddThis Social Bookmark Button