How can I do Databinding in c#?

c#, data-binding, winforms

Solution

You want

editBox.DataBindings.Add("Text", car, "Name");

The first parameter is the name of the property on the control that you want to be databound, the second is the data source, the third parameter is the property on the data source that you want to bind to.

Problem

I have the following class ``` public class Car { public Name {get; set;} } ``` and I want to bind this programmatically to a text box. How do I do that? Shooting in the dark: ``` ... Car car = new Car(); TextEdit editBox = new TextEdit(); editBox.DataBinding.Add("Name", car, "Car - Name"); ... ``` I get the following error "Cannot bind to the propery 'Name' on the target control. What am I doing wrong and how should I be doing this? I am finding the databinding concept a bit difficult to grasp coming from web-development.

Original source