views:

43

answers:

1

I'm trying to learn how to use WinForms databinding, but one thing that's confusing me is why you would "Add" a databinding rather then "Set" a databinding? For example consider the following code.

TextBox1.DataBindings.Add("Text", ds,"Customers.CustomerID")

As I understand it this line adds a databinding to TextBox1 that binds its Text property to the CustomerID fields in the Customers table of the ds DataSet.

Yet to me the term "Add" implies that you can add multiple of something. Which confuses me in this example. Why would you have a textbox bound to multiple things? How could that even be possible? Can someone provide an example where multiple bindings make sense?

+2  A: 

You can also bind other fields, such as Enabled.

TextBox1.DataBindings.Add("Enabled", myPresentationModel, "IsTextBox1Enabled");
Anna Lear
Oh man that's so obvious now thanks. So what happens if I bind twice to the same property? Like TextBox1.DataBindings.Add("Text", ds,"Customers.CustomerID"); and then TextBox1.DataBindings.Add("Text", ds,"Customers.CustomerName"). Does the 2nd binding to the Text property replace the first?
Eric Anastas
Off the top of my head, I'm pretty sure it throws an exception along the lines of "duplicate binding cannot be added". (Edit: Ah, there we go. http://msdn.microsoft.com/en-us/library/b6y3aby2(v=VS.100).aspx -- "The propertyName is already data-bound.")
Anna Lear