3

I can't seem to get basic databinding to work in the WinForms designer. (using VS2012)

  1. I create a new Win Forms Application project called DatabindTest.
  2. To this project I add a class called Class1.cs.
  3. In this class I create a (String) property called MyProperty along with a constructor that sets MyProperty to "abc".
  4. I build the solution.
  5. Using the Form1 designer, I add a Textbox to the form (textBox1).
  6. In the Properties of textBox1, I expand DataBindings and open the Advanced dialog. enter image description here
  7. I expand the Binding dropdown, and click Add Project Data Source..., and select the Object datasource. Then I expand the DatabindTest node and select Class1 as the Data Object. enter image description here
  8. I confirm that the Binding field now says "class1BindingSource - MyProperty" (as expected).
  9. In Form1.cs at the start of the Form1 class, I create a new instance of Class1 (see code below).
  10. At this point, I build and start the program. I expect to see "abc" in the textbox, but it is empty.

What have I done wrong?

public partial class Form1 : Form
{
    Class1 c1 = new Class1();
    public Form1()
    {
        InitializeComponent();
        //this.textBox1.Text = c1.MyProperty;   //if I uncomment this line, 
                                                //"abc" appears in textBox1
                                                //so why not through databinding?
    }
}
kmote
  • 16,095
  • 11
  • 68
  • 91

1 Answers1

4

You should set the DataSource of class1BindingSource:

class1BindingSource.ِDataSource = c1;

If you take a look at designer generated code for your text box and data-binding, you will see a code like this:

this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text",
                               this.class1BindingSource, "MyProperty", true));

As you can see, class1BindingSource is the data source of data-binding and you should pass data to its DataSource to show in the Text property of textBox1.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Ah, indeed that was the magic secret that eluded me. What puzzles me is, why doesn't the "Advanced" DataBindings wizard take care of this automatically? In fact, later on in the designer generated code, I find this line: `this.class1BindingSource.DataSource = typeof(DatabindTest.Class1);` which did nothing. Is there any way in the wizard to bind directly to a specific instance of an object? – kmote Jun 27 '16 at 23:10
  • 1
    `this.class1BindingSource.DataSource = typeof(DatabindTest.Class1);` It's just for designer Support. It lets the designer knows about the type which you want to use in data-binding. This way the designer will show a list of properties in data-binding section in property grid when you want to choose a property from list. – Reza Aghaei Jun 27 '16 at 23:12
  • The binding source is just an interface between your control and the real object. The binding source doesn't know from where you want to load data, so you can not expect it generate `class1BindingSource.ِDataSource = c1;` for you. Maybe you want to load data from a business logic class or from a service or somewhere else ... – Reza Aghaei Jun 27 '16 at 23:14
  • 2
    If you are creating a Data application, which you expect the designer generates more codes for you, you may find this [answer](http://stackoverflow.com/a/37824444/3110834) helpful. I personally prefer to not use data-set / table adapter approach anymore, but it's really rapid. – Reza Aghaei Jun 27 '16 at 23:17