Is there a better way for updating bound controls when updating the data source manually?
-
Hi I have a form that has text boxes and combos bound to fields in an object. When the user is using the form they have the option to apply some default values to some of the fields. I was hoping that I would just have to either update the control with the value or just update the field in the object. When I update the field in the object, the form will not refresh with the new values until another field on the form is changed. If I update the control, the field in the object does not update. My way around this was to update both like this
case "LOGCERT_LEVEL_":
logginCertCombo.SelectedValue = newValue;
staff.LogCert = newValue;
break;To me that is a bit ugly X| Is there a way to do this automatically? My controls are bound like this
logginCertCombo.DataBindings.Add("SelectedValue", staff, "LogCert");
I've tried google and today it is not being my friend. Many Thanks
The FoZ
-
Hi I have a form that has text boxes and combos bound to fields in an object. When the user is using the form they have the option to apply some default values to some of the fields. I was hoping that I would just have to either update the control with the value or just update the field in the object. When I update the field in the object, the form will not refresh with the new values until another field on the form is changed. If I update the control, the field in the object does not update. My way around this was to update both like this
case "LOGCERT_LEVEL_":
logginCertCombo.SelectedValue = newValue;
staff.LogCert = newValue;
break;To me that is a bit ugly X| Is there a way to do this automatically? My controls are bound like this
logginCertCombo.DataBindings.Add("SelectedValue", staff, "LogCert");
I've tried google and today it is not being my friend. Many Thanks
The FoZ
Sound like you want to look at the .BindingContext for your Form in order to get the BindingManagerBase (which will get you a Property Manager versus a CurrencyManager because it looks like you are binding to a single business object) so that you can use .PullData() and .PushData() to sync your controls and DataSource ...
-
Sound like you want to look at the .BindingContext for your Form in order to get the BindingManagerBase (which will get you a Property Manager versus a CurrencyManager because it looks like you are binding to a single business object) so that you can use .PullData() and .PushData() to sync your controls and DataSource ...
Thanks for you reply Gerry, but I'm not sure how to get this working. I've looked up the Currency and Property Manager Classes and where the classes contain the Push and Pull methods they are not visible. I created a
private CurrencyManager myFormCurrencyManager
for my form and addedmyFormCurrencyManager = (CurrencyManager)this.BindingContext[staff]
after I bound the controls. This caused an invalid cast exception so I changed it all to aPropertyManager
still with no luck. Am I missing something else? Do you know of any decent examples? CheersThe FoZ
-
Thanks for you reply Gerry, but I'm not sure how to get this working. I've looked up the Currency and Property Manager Classes and where the classes contain the Push and Pull methods they are not visible. I created a
private CurrencyManager myFormCurrencyManager
for my form and addedmyFormCurrencyManager = (CurrencyManager)this.BindingContext[staff]
after I bound the controls. This caused an invalid cast exception so I changed it all to aPropertyManager
still with no luck. Am I missing something else? Do you know of any decent examples? CheersThe FoZ
Sorry about that; didn't realize those methods were protected. Here is a solution that works: use the DataBindings ReadValue and WriteValue methods; eg. <pre>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { private TextBox fistNameTextBox = new TextBox() { Location = new Point( 0, 0 ) }; private TextBox lastNameTextBox = new TextBox() { Location = new Point( 0, 30 ) }; private Button readButtom = new Button() { Location = new Point( 0, 70 ), AutoSize = true, Text = "Read from Source" }; private Person aPerson = new Person(); public Form1() { readButtom.Click += new EventHandler( readButtom_Click ); fistNameTextBox.DataBindings.Add( new Binding( "Text", aPerson, "FirstName" ) ); lastNameTextBox.DataBindings.Add( new Binding( "Text", aPerson, "LastName" ) ); this.Controls.AddRange( new Control[] { fistNameTextBox, lastNameTextBox, readButtom } ); } void readButtom_Click( object sender, EventArgs e ) { aPerson.FirstName = "John"; aPerson.LastName = "Smith"; foreach ( Control control in this.FindForm().Controls ) { if ( control is TextBox && control.DataBindings.Count > 0 ) &n
-
Sorry about that; didn't realize those methods were protected. Here is a solution that works: use the DataBindings ReadValue and WriteValue methods; eg. <pre>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { private TextBox fistNameTextBox = new TextBox() { Location = new Point( 0, 0 ) }; private TextBox lastNameTextBox = new TextBox() { Location = new Point( 0, 30 ) }; private Button readButtom = new Button() { Location = new Point( 0, 70 ), AutoSize = true, Text = "Read from Source" }; private Person aPerson = new Person(); public Form1() { readButtom.Click += new EventHandler( readButtom_Click ); fistNameTextBox.DataBindings.Add( new Binding( "Text", aPerson, "FirstName" ) ); lastNameTextBox.DataBindings.Add( new Binding( "Text", aPerson, "LastName" ) ); this.Controls.AddRange( new Control[] { fistNameTextBox, lastNameTextBox, readButtom } ); } void readButtom_Click( object sender, EventArgs e ) { aPerson.FirstName = "John"; aPerson.LastName = "Smith"; foreach ( Control control in this.FindForm().Controls ) { if ( control is TextBox && control.DataBindings.Count > 0 ) &n