Refreshing control if entity is re-loaded

A

Andrus

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

Pavel Minaev

Andrus said:
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.
 
A

Andrus

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

Pavel Minaev

Andrus said:
Pavel,


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?
 

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