Do bound controls receive any notification when the bound item is swapped for a different item?

C

cjard

Suppose:

A TextBox is bound to a BindingSource, which is bound to a DataTable
A BindingNavigator is used to alter the current row being looked at by
the BindingSource
(i.e. Nav's NEXT button is pressed. BS.Position changes from 2 to 3,
Textbox was showing APPLE, now shows ORANGE)
Does the textbox receive any notification that this occurred? If so,
what?


I ask because I want to extend TextBox and add some extra properties.
When the textbox is showing an item off a DataRow whose .RowState is
Unchanged, i want to enable the TextBox. If the .RowState is Modified,
I want to make the TextBox disabled. If the .RowState is Added, I want
to make the Textbox ReadOnly.

If I can have the TextBox detect when the underlying row has changed
to another one, I can investigate the .RowState and change the state
of the TextBox accordingly.

Currently, I can implement this by hooking to the .PositionChanged
or .CurrentChanged events of the BindingSource, but I would prefer to
make it a TextBox internal thing

Thanks in advance
 
N

Nicholas Paldino [.NET/C# MVP]

Well, obviously the Binding instance updates the property on the textbox
when the value changes. There is no "special" notification that the textbox
receives though to change the property. From the textbox's point of view,
it's just another property change.

It's because of this that you don't want to make this a textbox-internal
thing because it's not the appropriate place.

What you are going to have to do is create a custom class which wraps a
DataRow instance. This class will expose a boolean property which indicates
whether or not the textbox should be enabled as well as implement the
INotifyPropertyChanged interface. The class should hook up to the
appropriate events on the data table indicating when the row has changed, or
the row state has changed, and then change its property value accordingly.
Of course, you would also fire the PropertyChanged event when the value
changes in response to the event.

Then, you can just bind the Enabled property of the TextBox to the
property you expose, and it should work.
 
C

cjard

Well, obviously the Binding instance updates the property on the textbox
when the value changes. There is no "special" notification that the textbox
receives though to change the property. From the textbox's point of view,
it's just another property change.
OK, maybe I dont have the data binding architecture correct in my head
then.. Previously I thought:

TextBox is databound
TextBox understands that it is databound
TextBox is told that the data model has changed
TextBox, being the View for the Model (MVC) requeries the model
TextBOx shows the new value from the model, in its view

Your comments indicate it is more like:

TextBox is databound
TextBox is ignorant of this
TextBox sits there and has values pushed into it by an externally
intelligent entity
This entity is not the model, but it receives notifications that the
model has changed, and pushes the changes into whatever viewing
controls are relevant
Changes to the textbox are communicated back to the entity.. how?
During Validation?

It's because of this that you don't want to make this a textbox-internal
thing because it's not the appropriate place.
OK, it makes more sense now that i can see textbox isnt intelligent
with regard to being a View in MVC concept.

What you are going to have to do is create a custom class which wraps a
DataRow instance.
You could say that I already have this, given that all my DataRow are
actually typed DataRow generated by .NET 2 DataSet Designer.
This class will expose a boolean property which indicates
whether or not the textbox should be enabled as well as implement the
INotifyPropertyChanged interface. The class should hook up to the
appropriate events on the data table indicating when the row has changed, or
the row state has changed, and then change its property value accordingly.
Of course, you would also fire the PropertyChanged event when the value
changes in response to the event.

Then, you can just bind the Enabled property of the TextBox to the
property you expose, and it should work.
I thought about everything a little more deeply and I've hit a bump:

A new row that enters the dataset will always be state Added
A row that was filled from DB is Unchanged, but becomes Modified upon
changes
At some point in its life, a textbox is going to have to be enabled
regardless of the underlying row state (this is used for searching
within the app - all fields must be enabled)

Taking the real example of a Primary key field:

When the row is added, the value is unknown as it is assigned by the
database.
The field will be Enabled = False, ReadOnly = True

When the row is browsed after db download, editing should be
disallowed, but copy and paste operations allowed
The field will be Enabled = True, ReadOnly = True

When the row is used for searching, we are going to construct a
dynamic SQL where clause from this row, so we dont care if the user
edits the PK field else how would they search by PK?
The field will be Enabled = true, ReadOnly = false


DataTable has scope to make its DataColumn readonly, but I think youre
right in that this should be a property of a row.. So i can add some
extra properties to my already-typed rows, and bind the TextBox values
for .ReadOnly and .Enabled to those props. The props, internal to
DataRow, can change because they have knowledge of whether a row is
added, unch/modded or for_searching (another separate property i think
i'll have to add, yes?)

My question/bump is.. Will .NET 2's BindingSource raise any relevant
events to allow the textbox' bindings to not only read a change of
e.g. text value to show, but a change in these properties too? - You
mentioned the propertychanged event - how is this used with a
BindingSource?
 

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