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. Question on simple binding

Question on simple binding

Scheduled Pinned Locked Moved C#
questionwpfwcf
11 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.
  • B Brett Slaski

    Can I simple-bind an object that derives from System.Windows.Forms.UserControls class? Thank you, Brett

    H Offline
    H Offline
    Heath Stewart
    wrote on last edited by
    #2

    Bind data to it, or bind it's properties to something else? Please be specific, as this is a rather ambiguous question. Both are possible, but rather than go into a length discussion of both, please be specific. Also take a look at the Control.DataBindings property in the .NET Framework SDK that gets installed by default with VS.NET.

    Microsoft MVP, Visual C# My Articles

    B 1 Reply Last reply
    0
    • H Heath Stewart

      Bind data to it, or bind it's properties to something else? Please be specific, as this is a rather ambiguous question. Both are possible, but rather than go into a length discussion of both, please be specific. Also take a look at the Control.DataBindings property in the .NET Framework SDK that gets installed by default with VS.NET.

      Microsoft MVP, Visual C# My Articles

      B Offline
      B Offline
      Brett Slaski
      wrote on last edited by
      #3

      Thanks for the response. I has more of an explaination here but didn't receive any replies. I have created a UserControl which contains a ComboBox. This combo box if filled with data in the controls constructor. I created a public property to expose the ComboBox.SelectedValue property so I may bind to it from forms which use the control. The bindings never work. The debugger doesn't compain at all, and the values in the UserControl's ComboBox never changes with the CurrencyManager.Position changes.

      H 1 Reply Last reply
      0
      • B Brett Slaski

        Thanks for the response. I has more of an explaination here but didn't receive any replies. I have created a UserControl which contains a ComboBox. This combo box if filled with data in the controls constructor. I created a public property to expose the ComboBox.SelectedValue property so I may bind to it from forms which use the control. The bindings never work. The debugger doesn't compain at all, and the values in the UserControl's ComboBox never changes with the CurrencyManager.Position changes.

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #4

        Using the DataBindings property like so doesn't work?:

        myUserControl1.DataBindings.Add("SelectedValue", dataSet1, "DataTable1");

        Microsoft MVP, Visual C# My Articles

        B 1 Reply Last reply
        0
        • H Heath Stewart

          Using the DataBindings property like so doesn't work?:

          myUserControl1.DataBindings.Add("SelectedValue", dataSet1, "DataTable1");

          Microsoft MVP, Visual C# My Articles

          B Offline
          B Offline
          Brett Slaski
          wrote on last edited by
          #5

          Correct, that binding will not work, hence my dilemma. Neither of these work: myUserControl1.DataBindings.Add("SelectedValue", dataSet1, "DataTable1"); myUserControl1.DataBindings.Add( new System.Windows.Forms.Bindings("SelectedValue", dataSet1, "DataTable1")); Note, setting a value directly to the UserControl property does work. Thanks for your assistence, Brett

          H 1 Reply Last reply
          0
          • B Brett Slaski

            Correct, that binding will not work, hence my dilemma. Neither of these work: myUserControl1.DataBindings.Add("SelectedValue", dataSet1, "DataTable1"); myUserControl1.DataBindings.Add( new System.Windows.Forms.Bindings("SelectedValue", dataSet1, "DataTable1")); Note, setting a value directly to the UserControl property does work. Thanks for your assistence, Brett

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #6

            Both of those overloads to the same thing. The first actually uses the params to construct a Binding instance and then calls the second overload. The thing about binding contexts is that they must be bound exactly the same. So, if you bind a ComboBox to a DataTable, then the Binding must also be bound to a DataTable. I threw together a quick example. The ComboBox.DataSource is bound to a DataTable (in a DataSet). The DisplayMember and ValueMember specify column names (say, "ID" and "Name"). I also defined a property (SelectedValue) that reflects ComboBox.SelectedValue and created a binding in my ctor like so:

            this.DataBindings.Add("SelectedValue", dataSet1, "Table1.ID");

            The really important part is how you get the binding context. For the back and previous buttons, I threw together this:

            private void button1_Click(object sender, System.EventArgs e)
            {
            CurrencyManager cm = (CurrencyManager)this.BindingContext
            [this.dataSet1.Table1]; // typed dataset, so table is a prop
            if (cm != null)
            if (cm.Position >= 1) cm.Position--;
            }

            private void button2_Click(object sender, System.EventArgs e)
            {
            CurrencyManager cm = (CurrencyManager)this.BindingContext
            [this.dataSet1.Table1]; // typed dataset, so table is a prop
            if (cm != null)
            if (cm.Position < cm.Count - 1) cm.Position++;
            }

            Notice that the BindingContext is the table itself - the same as was bound to the ComboBox.

            Microsoft MVP, Visual C# My Articles

            B 1 Reply Last reply
            0
            • H Heath Stewart

              Both of those overloads to the same thing. The first actually uses the params to construct a Binding instance and then calls the second overload. The thing about binding contexts is that they must be bound exactly the same. So, if you bind a ComboBox to a DataTable, then the Binding must also be bound to a DataTable. I threw together a quick example. The ComboBox.DataSource is bound to a DataTable (in a DataSet). The DisplayMember and ValueMember specify column names (say, "ID" and "Name"). I also defined a property (SelectedValue) that reflects ComboBox.SelectedValue and created a binding in my ctor like so:

              this.DataBindings.Add("SelectedValue", dataSet1, "Table1.ID");

              The really important part is how you get the binding context. For the back and previous buttons, I threw together this:

              private void button1_Click(object sender, System.EventArgs e)
              {
              CurrencyManager cm = (CurrencyManager)this.BindingContext
              [this.dataSet1.Table1]; // typed dataset, so table is a prop
              if (cm != null)
              if (cm.Position >= 1) cm.Position--;
              }

              private void button2_Click(object sender, System.EventArgs e)
              {
              CurrencyManager cm = (CurrencyManager)this.BindingContext
              [this.dataSet1.Table1]; // typed dataset, so table is a prop
              if (cm != null)
              if (cm.Position < cm.Count - 1) cm.Position++;
              }

              Notice that the BindingContext is the table itself - the same as was bound to the ComboBox.

              Microsoft MVP, Visual C# My Articles

              B Offline
              B Offline
              Brett Slaski
              wrote on last edited by
              #7

              Thank you. Before I dive into changing some things around, I have a question on the BindingContext. There are 3 TextBoxes and 2 UserControls which are bound to the same DataSet.Table (fyi, my dataset is typed). Would the way I get the BindingContext effect the binding between these three different controls? In other words, the binding is working fine on the the 3 TextBoxes but is not working on the 2 UserControls. If I: string o=""; BindingManagerBase bindingManager = this.BindingContext[DataSet, "table"]; foreach(Binding b in bindingManager.Bindings) { o += b.Control.ToString() + "\n"; } MessageBox.Show(o); The TextBox binding show, but the UserControls do not. And obviously, the Text boxes are bound and change with .Position property. Is there anything other that a set and get required on the UserControl for the binding to work?

              H 1 Reply Last reply
              0
              • B Brett Slaski

                Thank you. Before I dive into changing some things around, I have a question on the BindingContext. There are 3 TextBoxes and 2 UserControls which are bound to the same DataSet.Table (fyi, my dataset is typed). Would the way I get the BindingContext effect the binding between these three different controls? In other words, the binding is working fine on the the 3 TextBoxes but is not working on the 2 UserControls. If I: string o=""; BindingManagerBase bindingManager = this.BindingContext[DataSet, "table"]; foreach(Binding b in bindingManager.Bindings) { o += b.Control.ToString() + "\n"; } MessageBox.Show(o); The TextBox binding show, but the UserControls do not. And obviously, the Text boxes are bound and change with .Position property. Is there anything other that a set and get required on the UserControl for the binding to work?

                H Offline
                H Offline
                Heath Stewart
                wrote on last edited by
                #8

                Again, take a look at the BindingContext property documentation in the .NET Framework SDK. It's ncessary that you bind properties to the same data source as the BindingContext. For example, many people will bind a DataTable to a DataGrid (assign it to DataSource) instead of setting the DataSource property to the DataSet, and the table name (typed or not) as the DataMember name. If you then use specify a context like BindingContext[_dataSet1_._dataTable1_], it will not work. A good way is to use BindingContext[_dataGrid1_.DataSource, _dataGrid1_.DataMember]. This will make sure that the binding contexts are the same and correct.

                Microsoft MVP, Visual C# My Articles

                B 1 Reply Last reply
                0
                • H Heath Stewart

                  Again, take a look at the BindingContext property documentation in the .NET Framework SDK. It's ncessary that you bind properties to the same data source as the BindingContext. For example, many people will bind a DataTable to a DataGrid (assign it to DataSource) instead of setting the DataSource property to the DataSet, and the table name (typed or not) as the DataMember name. If you then use specify a context like BindingContext[_dataSet1_._dataTable1_], it will not work. A good way is to use BindingContext[_dataGrid1_.DataSource, _dataGrid1_.DataMember]. This will make sure that the binding contexts are the same and correct.

                  Microsoft MVP, Visual C# My Articles

                  B Offline
                  B Offline
                  Brett Slaski
                  wrote on last edited by
                  #9

                  I am sorry, I don't see how this answers my question. The combobox bindings are being handled in the control. It is the 'simple data binding' to the UserControl's property I can't get to work. I don't think I would pull a BindingContext from [UserControl.myProperty] would I? I don't think I would get a useful binding manager that way. Thank you very much for helping me through this.

                  H 1 Reply Last reply
                  0
                  • B Brett Slaski

                    I am sorry, I don't see how this answers my question. The combobox bindings are being handled in the control. It is the 'simple data binding' to the UserControl's property I can't get to work. I don't think I would pull a BindingContext from [UserControl.myProperty] would I? I don't think I would get a useful binding manager that way. Thank you very much for helping me through this.

                    H Offline
                    H Offline
                    Heath Stewart
                    wrote on last edited by
                    #10

                    As I said in my previous reply[^], the binding contexts have to be exactly the same object with exactly the same data member (like a table name or collection property name or something). You can't bind to a DataTable in a DataSet in one case when the other binding is to the DataSet itself. I wrote a quick example and it works fine. If you reply to this via email (use the email link) I can grab your email address and send it to you to examine the source (you won't be able to run it since the connection string and command are specific to our database here, but you can always change it).

                    Microsoft MVP, Visual C# My Articles

                    B 1 Reply Last reply
                    0
                    • H Heath Stewart

                      As I said in my previous reply[^], the binding contexts have to be exactly the same object with exactly the same data member (like a table name or collection property name or something). You can't bind to a DataTable in a DataSet in one case when the other binding is to the DataSet itself. I wrote a quick example and it works fine. If you reply to this via email (use the email link) I can grab your email address and send it to you to examine the source (you won't be able to run it since the connection string and command are specific to our database here, but you can always change it).

                      Microsoft MVP, Visual C# My Articles

                      B Offline
                      B Offline
                      Brett Slaski
                      wrote on last edited by
                      #11

                      I tried someting new. I created a new UserControl, but instead of inheriting from System.Windows.Forms.UserControl I inherited from System.Windows.Forms.ComboBox (isn't this now a customcontrol??) Anyway this new user control binds without any problems. I bind to lp2.SelectedValue Here are the basics. public class lp2 : System.Windows.Forms.ComboBox { // Our Variables private SqlDataAdapter dataAdapter; private SqlConnection sqlConnection; private DataSet dataset; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public lp2() { // This call is required by the Windows.Forms Form Designer. InitializeComponent(); // TODO: Add any initialization after the InitializeComponent call sqlConnection = Utilities.dbUtilities.getWebDbCn(); string selectCommand = "select * from lang"; dataset = new DataSet(); dataAdapter = new SqlDataAdapter(selectCommand, sqlConnection); dataAdapter.Fill(dataset, "lang"); this.DataSource = dataset; this.DisplayMember = "lang.langText"; this.ValueMember = "lang.lang"; } } My other UserControl is simular except I have a ComboBox added to the class where this class inherits ComboBox. Now I guess I need to figure out how to get the older UserControl to work since I will be back to the issue when I have a UserControl with more than one control or component on it. Are there things I must do in a UserControl to make sure I can bind to it's properties?

                      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