How can I do Databinding in c#?

0 votes
asked Sep 22, 2008 by xerx

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.

10 Answers

0 votes
answered Sep 22, 2008 by will

You're trying to bind to the "Name" of the TextEdit control. The name is used for accessing the control programmatically, and cannot be bound against. You should be binding against the Text of the control.

0 votes
answered Sep 22, 2008 by danimal

Without looking at the syntax, I'm pretty sure it's:

editBox.DataBinding.Add("Text", car, "Name");
0 votes
answered Sep 22, 2008 by ageektrapped

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.

0 votes
answered Sep 22, 2008 by tcks

Try:

editBox.DataBinding.Add( "Text", car", "Name" );
0 votes
answered Sep 22, 2008 by itsmatt

I believe that

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

should do the trick. Didn't try it out, but I think that's the idea.

0 votes
answered Sep 22, 2008 by romain-verdier
editBox.DataBinding.Add("Text", car, "Name");

First arg is the name of the control property, the second is the object to bind, and the last, the name of the object property you want to use as the data source.

0 votes
answered Sep 22, 2008 by john-hunter

You are quite close the data bindings line would be

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

This first parameter is the property of your editbox object that will be data bound. The second parameter is the data source you are binding to and the last parameter is the property on the data source that you want to bind to.

Bear in mind that the data binding is one way so if you change the edit box then the car object gets updated but if you change the car name directly the edit box is not updated.

0 votes
answered Sep 3, 2016 by tofo

The following is generic class that can be used as a property and implements INotifyPropertyChanged used by bound controls to capture changes in the property value.

public class NotifyValue<datatype> : INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    datatype _value;
    public datatype Value
    {
        get
        {
            return _value;
        }
        set
        {
            _value = value;
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Value"));
        }
    }

}

It can be declared like this:

public NotifyValue<int> myInteger = new NotifyValue<int>();

and assigned to a textbox like this

Textbox1.DataBindings.Add(
    "Text", 
    this, 
    "myInteger.Value", 
    false, 
    DataSourceUpdateMode.OnPropertyChanged
);

..where "Text" is the property of the textbox, 'this' is current Form instance.

A class does not have to inherit the INotifyPropertyChanged class. Once you declare an event of type System.ComponentModel.PropertyChangedEventHandler the class change event will be subscribed to by the controls databinder

0 votes
answered Sep 15, 2017 by ramgy-borja

it's

 this.editBox.DataBindings.Add(new Binding("Text", car, "Name"));
0 votes
answered Sep 15, 2017 by graviton

Using C# 4.6 syntax:

editBox.DataBinding.Add(nameof(editBox.Text), car, nameof(car.Name));

if car is null, then the above code will fail in a more conspicuous way than using literal string to represent the datamember of car

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...