Post by Marco Schw » Sat, 15 Sep 2007 22:30:03

I try to use Databinding with the BindingList. So I write a class
Person(Name, ReadOnly) and a PersonList : BindingList<Person>.

In my form I have two controls (TextBox and CheckBox) in the
MainFormLoad function I binding as follow:

void MainFormLoad(object sender, EventArgs e)
personList = new PersonList();
cbxPersonReadOnly.DataBindings.Add("Checked", personList, "ReadOnly");
txtPersonName.DataBindings.Add("Text", personList, "Name");
txtPersonName.DataBindings.Add("ReadOnly", personList, "ReadOnly");

My question:

I click on the checkbox (ReadOnly) and the TextBox don't changed. Then
click on Next or Prev and the TextBox.ReadOnly is changed.

Can someone says me, what I do wrong?



Post by Marco Schw » Sat, 15 Sep 2007 22:44:11

> My question:
Sorry, now it works correctly. Perhaps I run a old version of the




Post by Marco Schw » Sat, 15 Sep 2007 23:24:13

Marco Schwarz schrieb:
Only with the Event

void CbxPersonReadOnlyClick(object sender, EventArgs e)
txtPersonName.ReadOnly = cbxPersonReadOnly.Checked;

it works fine.

Have someone an idea?



Post by Morten Wen » Sun, 16 Sep 2007 16:50:23

n Fri, 14 Sep 2007 15:30:03 +0200, Marco Schwarz <> wrote:

Hi Marco,

It's a combination of things that is causing your TextBox to update late..

First, the default databinding behaviour is to update its underlying source once you validate the control for instance by leaving it. Since you want the TextBox to gray out immediatly as you click the CheckBox you need to tell the databinding to update immediatly (see the code below on how to do this).

Second, since you bind two controls to the same property the second control won't get notified, and won't get updated until you cange the datasource by selecting another item or similar. To fix this, Person needs to implement the INotifyPropertyChanged event, and fire an event every time this property is changed. This is also true for any property you change programmatically (see the code below on how to implement and use INotifyPropertyChanged)

public partial class Form2 : System.Windows.Forms.Form

public Form2()

protected override void OnLoad(EventArgs e)
// You probably don't need a PersonList class
BindingList<Person> personList = new BindingList<Person>();
personList.Add(new Person("One"));
personList.Add(new Person("Two"));
personList.Add(new Person("Three"));

// The ListBox will allow us to change persons and see the effect of setting ReadOnly
listBox1.DataSource = personList;
listBox1.DisplayMember = "Name";

// Note the added parameters for the Checked property
// Setting DataSourceUpdateMode.OnPropertyChanged means the Person will get updated immediatly.
// This is also good on TextBoxes where you want to update something while you write.
checkBox1.DataBindings.Add("Checked", personList, "ReadOnly", false, DataSourceUpdateMode.OnPropertyChanged);
textBox1.DataBindings.Add("Text", personList, "Name");
textBox1.DataBindings.Add("ReadOnly", personList, "ReadOnly");

public class Person : INotifyPropertyChanged
private bool readOnly;

public bool ReadOnly
get { return readOnly; }
readOnly = value;
// Fire a PropertyChanged event so the TextBox will get notified
PropertyChanged(this, new PropertyChangedEventArgs("Locked"));

private string name;

public string Name
get { return name; }
set { name = value; }

public Person(string name)
this.Name = name;

// This is the code added by SHIFT-ALT-F10 on INotifyPropertyChanged
// All this interface does is declare a single event
#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;


Happy coding!
Morten Wennevik [C# MVP]


Post by Marco Schw » Sun, 16 Sep 2007 19:39:02

Thanks, it work fine