---
title: "Simple data binding simplified using [BindToField] and [BindToValueList]"
url: "https://osherove.com/blog/2004/8/30/simple-data-binding-simplified-using-bindtofield-and-bindtov.md"
canonical: "https://osherove.com/blog/2004/8/30/simple-data-binding-simplified-using-bindtofield-and-bindtov.html"
published: "2004-08-30T19:38:00"
updated: "2004-08-30T19:38:00"
tags: [".Net Original"]
author: "Roy Osherove"
description: "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!…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

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

By Roy Osherove August 30, 2004 In [.Net Original](https://osherove.com/blog/category/net-original)

**Historical article** · Originally published August 30, 2004; technical details may be outdated.

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](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.

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- Sep 3, 2004

    ### [Testing GUIs and customer tests using FIT](https://osherove.com/blog/2004/9/3/testing-guis-and-customer-tests-using-fit.html)

- Sep 1, 2004

    ### [Sunday at the VB.Net users group: Software Architecture: Techniques, Methods, and Issues](https://osherove.com/blog/2004/9/1/sunday-at-the-vbnet-users-group-software-architecture-techni.html)

- Aug 26, 2004

    ### [Revisited: Killing a process, Introducing: TaskKiller](https://osherove.com/blog/2004/8/26/revisited-killing-a-process-introducing-taskkiller.html)

- Aug 23, 2004

    ### [Tomorrow is DBA day, and .Net rocks is back on my mind](https://osherove.com/blog/2004/8/23/tomorrow-is-dba-day-and-net-rocks-is-back-on-my-mind.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
