471,356 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,356 software developers and data experts.

Refreshing control if entity is re-loaded

Winforms appl creates customer edit form containing textboxes using

Customer dataSource = Northwind.Customers.GetByName("Airbus");
MaskedTextBox tb = new MaskedTextBox();
Binding binding = new Binding("Text", dataSource, "Phone");
binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
binding.FormattingEnabled = true;
tb.DataBindings.Add(binding);

There is revert button in form which discards edited fields read original
values from database.
For this I use the following code:

// Re-Get original value
dataSource = Northwind.Customers.GetByName("Airbus");

However edit form still shows edited phone number.

How to force MaskedTextBox or other Control to update its contents if bound
entity (customer) is re-loaded form database ?

Andrus.

Sep 4 '08 #1
3 1928

"Andrus" <ko********@hot.eewrote in message
news:ON**************@TK2MSFTNGP06.phx.gbl...
Winforms appl creates customer edit form containing textboxes using

Customer dataSource = Northwind.Customers.GetByName("Airbus");
MaskedTextBox tb = new MaskedTextBox();
Binding binding = new Binding("Text", dataSource, "Phone");
binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
binding.FormattingEnabled = true;
tb.DataBindings.Add(binding);

There is revert button in form which discards edited fields read original
values from database.
For this I use the following code:

// Re-Get original value
dataSource = Northwind.Customers.GetByName("Airbus");

However edit form still shows edited phone number.
That's because you didn't change the actual data source. You've only changed
the value of your own variable (or field) dataSource, which is of type
"reference to Customer". It no longer points to the old instance of
Customer, but your binding still does.

Instead, try binding your controls to a BindingSource, and changing its
DataSource property.
Sep 5 '08 #2
Pavel,
Instead, try binding your controls to a BindingSource, and changing its
DataSource property.
Thank you.
I switched to BindingSource:

var BindingSource = new BindingSource();
BindingSource.DataSource = Customer;
Binding binding = new Binding("Text", BindingSource, "Phone");
binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
binding.FormattingEnabled = true;
control.DataBindings.Add(binding);

To revert changes I use

BindingSource.DataSource = OriginalCustomer;
BindingSource.ResetBindings(false);

Now control show old value properly after revert.

When another value is entered to TextBox after revert, this value is no more
written to customer property.
It seems that bindings are lost.
How to set DataSource so that bindings are not lost ?

Andrus.

Sep 5 '08 #3
"Andrus" <ko********@hot.eewrote in message
news:uo**************@TK2MSFTNGP06.phx.gbl...
Pavel,
>Instead, try binding your controls to a BindingSource, and changing its
DataSource property.

Thank you.
I switched to BindingSource:

var BindingSource = new BindingSource();
BindingSource.DataSource = Customer;
Binding binding = new Binding("Text", BindingSource, "Phone");
binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
binding.FormattingEnabled = true;
control.DataBindings.Add(binding);

To revert changes I use

BindingSource.DataSource = OriginalCustomer;
BindingSource.ResetBindings(false);

Now control show old value properly after revert.

When another value is entered to TextBox after revert, this value is no
more written to customer property.
It seems that bindings are lost.
No, the values are written. They are just written to the instance of
Customer that OriginalCustomer references, and I guess that you're looking
at the other one.

Can you please show where Customer and OriginalCustomer come from, and how
do you use them? In particular, how you check that the value is not written?
Sep 5 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Philo Del Middleston | last post: by
1 post views Thread by Mark Johnson | last post: by
1 post views Thread by Microsoft | last post: by
5 posts views Thread by Jensen Bredal | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.