Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Simple Alternative to Windows Forms Data Binding

0.00/5 (No votes)
10 Apr 2008 1  
An open source library of Windows Forms controls that update themselves automatically.

Introduction

While data binding is a powerful and convenient method for rendering database and object data in Windows Forms controls, it can get complex. The Update Controls library adopts a simpler approach. Instead of binding control data to properties, these controls fire events to get their data. An event can call whatever business logic is required. The control keeps track of the data that was accessed by that business logic, and when any of that data changes, the control fires the event again to update itself.

The GetText event

The UpdateTextBox control is a simple example. It fires an event called GetText that returns a string. You handle this event by returning the string that the text box should display.

private string firstNameTextBox_GetText()
{
    return _person.FirstName;
} 

Whenever the FirstName property of the person is changed, the control will fire the event again to get the new text.

But controls don’t have to be bound directly to data. They can call business logic. For example, a label that displays the person’s full name would have this GetText event.

private string fullNameLabel_GetText()
{
    return _person.FullName;
}

Where the Person object defines FullName like this.

public string FullName
{
    get
    {
        return string.Format("{0} {1}", FirstName, LastName);
    }
} 

Now when either FirstName or LastName changes, the full name label updates itself.

The SetText event

When the user edits the text in an UpdateTextBox, it fires an event called SetText. As you can imagine, this one takes the string that the user just entered.

private void firstNameTextBox_SetText(string value)
{
    _person.FirstName = value;
}

Because this event changes the FirstName property, it causes the full name label to update itself.

But wait! I didn’t tell it that the first name text box and the full name label were related. There’s no code in there to force the full name to update when the first name is changed. You don’t need that code, because the Update Controls library figures out the relationship on its own.

The Dynamic sentry

The bit of magic that makes that happen is the Dynamic sentry. This object sits next to each of your data fields and monitors all access to that data. The Person object has two: one for first name and one for last name.

private string _firstName;
private string _lastName;

#region Dynamic properties
// Generated by Update Controls --------------------------------
private Dynamic _dynFirstName = new Dynamic();
private Dynamic _dynLastName = new Dynamic();

public string FirstName
{
    get { _dynFirstName.OnGet(); return _firstName; }
    set { _dynFirstName.OnSet(); _firstName = value; }
}

public string LastName
{
    get { _dynLastName.OnGet(); return _lastName; }
    set { _dynLastName.OnSet(); _lastName = value; }
}
// End generated code --------------------------------
#endregion 

Every time you get the value of a data field, you call its sentry’s OnGet. And every time you change it, you call OnSet. But don’t worry about writing all of this sentry code yourself. The Update Controls library includes a Visual Studio add-in that generates C# or VB code for data fields. Just select the fields you want to expose and hit Ctrl+D, G.

Just before Update Controls fires a Get event like GetText, it pushes a monitor to the stack. When you call Dynamic.OnGet, it looks at the stack to find that monitor. At this point, it knows that that control depends upon that data. After the event ends, it pops that monitor off the stack and remembers the set of Dynamic sentries that were accessed.

Then, when you call Dynamic.OnSet, it looks up all of the controls that accessed that sentry. It now knows that all of those controls are out-of-date. When you’re finished making all of your changes, each of those controls fires its Get event to update itself.

Other control types

The Update Controls library wraps all of the common Windows Forms controls. Each of these control types have one or more Get events that return the type of data that they display. For example, the UpdateCheckBox fires a GetChecked event that returns a Boolean.

Some controls display more than one piece of data. These fire several Get events for the different kinds of data they display. The UpdateListBox, for example, first fires GetItems to get a list of objects, then fires GetItemText for each item to get the text to display.

Most controls also fire a GetEnabled event. This event returns true when the control should be enabled, and false when it should be disabled. If the event is not handled, then the control is always enabled. This makes it easy to enable and disable controls in response to changes in other controls.

Try it out

Download the source code or binary installer from http://codeplex.com/updatecontrols. Then open the Visual Studio project attached to this article. I started some class-level documentation of the library at http://updatecontrols.net/cs/documentation.shtml. If you have any questions, send me an email at support@updatecontrols.net.

The Update Controls library is licensed under the LGPL. The QuickDemo example application is licensed under the Code Project Open License.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here