Binding object

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

Here I use the Binding object to bind the text property of a TextBox control
txtName to the Name property of the product object

Product product = new Product("Apple", 17.19, 2);
Binding binding = new Binding("Text", product, "Name", true);
txtName.DataBindings.Add(binding);

The Binding object requires the following information:
- Name of the property of the control to which the binding is to done (e.g
Text Property
of a TextBox).
- Data source (the object product in the above example)
- The name of the property in the data source to which the property of the
control is to
be bound (Name in the above example, product.Name)

But I can also write in this way and skip the Binding object.
txtName.DataBinding.Add("Text", new Product("Apple", 17.19, 2), "Name");

Now to my question as you can see here I didn't use the Binding object at
all in the last example
so when is it nesessary to use this Binding object that I used in the first
example ?

//Tony
 
Product product = new Product("Apple", 17.19, 2);
Binding binding = new Binding("Text", product, "Name", true);
txtName.DataBindings.Add(binding);

But I can also write in this way and skip the Binding object.
txtName.DataBinding.Add("Text", new Product("Apple", 17.19, 2), "Name");

Now to my question as you can see here I didn't use the Binding object at
all in the last example
so when is it nesessary to use this Binding object that I used in the first
example ?

The latter version of Add constructs a Binding object for you from the
information provided, and returns it to you. If you're not going to use
the Binding object you can simply ignore the return value. So all in
all, it provides the same functionality, with less plumbing.

The end result is the same, only the road to it differs.
 
Back
Top