Simple data binding simplified using [BindToField] and [BindToValueList]

In one of the projects I'm on we're doing lots and lots of Winforms data binding. One of the forms is composed of 12 different tabs each with about 2 dozen controls and grids and what not (don't kill the messenger! that's what porting an old system looks like...) all needing to be bound to multiple datasets and data views.*shudder*
 
I can tell you this much - with this much controls on one form - your code and especially binding code is gonna get really ugly real fast. especially tedious . Oh, its nothing too hard - just lots of the same work done on a different target and source every time.
 
one of the things we've come up with to ease our pain is a simple Custom attribute that you can put on a control instance on the form (TextBox for example) that has some meta-data as to what data binding it will use at run time.
since this is not the simple lovable drag and drop binding GUI demos you see at conferences, you only really have the data you want at runtime and no way to do binding at design time - this is a very simple way to make a lot of that tedious code disappear. Here's how it looks on our form:
 
public class Form1 : System.Windows.Forms.Form
 {
  [BindToField("Products.ProductName")]
  public   System.Windows.Forms.TextBox txtName;
 
  [BindToValueList("Categories.CategoryName","CategoryID")]
  public System.Windows.Forms.ComboBox cmbCategories;
 
What we actually do at run time is do a very simple call to a bind helper class:
public Form1()
  {
  
   InitializeComponent();
   new SimpleDataComponent().FillNorthwindData(m_dataset);
 
   //this is the simple way
   ControlBinder.BindControls(this,m_dataset);
 
 The binder will automatically seek out all the controls with the custom attribute on them and bind them automatically to the appropriate data source. for controls that display lists of values (appropriately derived from ListControl) we have a "BindToValueListAttribute" for that purpose.
Anyway - thought you might like to save some time on your own so feel free to download this code directly from here: http://dev.magen.com/Agile/Code/ControlBinder.zip
it comes with source and a sample project.
 
The nice thing is that the attribute and binder object have the ability to let you specify more advanced properties for binding. for example (none of these are shown in this post)-
  • you can specific to exactly what property on the control your data source will be bound to.
  • you can put multiple attributes on a control to add multiple data bindings to it.
  • you can add a category to an attribute and have the binder bind only to controls that posses this property. this allows you to have multiple data sources (which can be multiple data views for example) on one form, each bound to a different set of controls on the form, differentiate by category names on the attributes of the controls.
This is a great exercise in using Reflection - which I have to say I'm falling for more and more each time I use it. So elegant and simple. I love it. Feel free to add comments and or requests and or bugs on this post.
Published Tuesday, August 31, 2004 1:38 AM by RoyOsherove
Filed under:

Comments

Tuesday, August 31, 2004 2:46 AM by TrackBack

# VERY Cool Winforms Data Binding Helper

VERY Cool Winforms Data Binding Helper
Tuesday, August 31, 2004 4:14 AM by TrackBack

# Databinding helper for winforms

Tuesday, August 31, 2004 11:25 AM by Oskar Austegard

# re: Simple data binding simplified using [BindToField] and [BindToValueList]

Very interesting. I tried the sample project, but it appears the combo box's value is not bound to each record.

I'd be very interested to see the code behind [sic] one of your more complex forms.

Thanks,
Tuesday, August 31, 2004 3:11 PM by David Hayden

# re: Simple data binding simplified using [BindToField] and [BindToValueList]

Over the past 3 months I have realized the power of attributes and reflection. I now use them for simple data validation on busines objects and thay have really simplified my programming. Not only is the code cleaner, but code changes are easier and the code is more self-documenting.

Your example on using attributes for simple data binding is just another great example of their use. Great stuff.
Wednesday, September 01, 2004 2:38 AM by TrackBack

# RE: Simple data binding simplified using [BindToField] and [BindToValueList]

Saturday, September 04, 2004 9:34 PM by Kenneth Russo

# re: Simple data binding simplified using [BindToField] and [BindToValueList]

Slick. I did something similar in a recent project, but used the Tag property instead. I'm wondering if I'm missing some goodness by using the Tag property instead of setting an attribute. I like seeing the setting in the Property Grid when I click on a component, but I'm certainly not married to the idea if there's a good reason to switch about which I'm ignorant.
Wednesday, September 22, 2004 5:22 AM by TrackBack

# ANN: Optimized automatic control binder is out - and its fast!

Wednesday, September 22, 2004 1:24 PM by Jerrad Anderson

# re: Simple data binding simplified using [BindToField] and [BindToValueList]

will this also work on ASP.net forms??? should right?
Friday, December 22, 2006 10:06 AM by Shun Reeda

# re: Simple data binding simplified using [BindToField] and [BindToValueList]

Hi:

I can´t download the source code, could you send it or post on other web page?, I know is too much to ask.

Sorry and thanks in advanced.

e-mail: shun_reeda@yahoo.com.mx

Thursday, August 23, 2007 1:58 PM by ISerializable - Roy Osherove's Blog

# ANN: Optimized automatic control binder is out - and its fast!

Last month I posted about a cool way we came up with to automatically bind all the controls on our application