Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Is there a better way for updating bound controls when updating the data source manually?

Is there a better way for updating bound controls when updating the data source manually?

Scheduled Pinned Locked Moved C#
questionannouncement
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheFoZ
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • T TheFoZ

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

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

      T 1 Reply Last reply
      0
      • L Lost User

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

        T Offline
        T Offline
        TheFoZ
        wrote on last edited by
        #3

        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 added myFormCurrencyManager = (CurrencyManager)this.BindingContext[staff] after I bound the controls. This caused an invalid cast exception so I changed it all to a PropertyManager still with no luck. Am I missing something else? Do you know of any decent examples? Cheers

        The FoZ

        L 1 Reply Last reply
        0
        • T TheFoZ

          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 added myFormCurrencyManager = (CurrencyManager)this.BindingContext[staff] after I bound the controls. This caused an invalid cast exception so I changed it all to a PropertyManager still with no luck. Am I missing something else? Do you know of any decent examples? Cheers

          The FoZ

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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

          T 1 Reply Last reply
          0
          • L Lost User

            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

            T Offline
            T Offline
            TheFoZ
            wrote on last edited by
            #5

            Thanks Gerry. The foreach loop was exactly the thing I was after

            foreach ( Control control in this.FindForm().Controls )
            { if ( control is TextBox && control.DataBindings.Count > 0 )
            control.DataBindings[ 0 ].ReadValue(); }

            Cheers

            The FoZ

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups