Sunday, October 10, 2004

WinForms UserControl DataBinding

Last week I worked on a UserControl which provided a bindable property. Our GUI developer bound it to a DataTable's column, but what happened at runtime was that while binding the property worked just fine, the bound DataRow's RowState was set to "Modified" each time, even when no user input had occurred on the control. In this case the RowState was expected to stay as "Unchanged".

I spent hours on searching the internet, until I finally found the reason why: There seems to be an undocumented naming convention inside .NET DataBinding: One must provide a public event of the following kind...

public event EventHandler PropertynameChanged;

... and fire the event when the bound property is set:

public object Propertyname {
    get {
        // return the property value
    }
    set {
        // set the property value,
        // then fire the event (in case the value changed)
        if (PropertynameChanged != null) {
            PropertynameChanged(this, EventArgs.Empty);
        }
    }
}


Hello, can someone there at MSDN please update the DataBinding documentation? Or did I miss something?