BindingSource and BindingNavigator

These are two controls that, unless you’re familiar with .NET, will not seem too familiar. Having said that, once you’ve used them, they’re pretty simple to understand.

The first class we’ll look at is BindingSource. Essentially, this class is intended to act as a source of data for any controls that are data bound. True, you can bind controls directly to DataSet and DataTable instances, but what happens if you want to change the underlying data source? You can potentially end up reconfiguring a lot of controls. By using a BindingSource as a kind of ‘middle man’ you can make your applications more maintainable.

The most important property of the BindingSource is the DataSource property. You’ll need to set this to your data object, be it a DataSet, DataTable or generic collection. If you’re using a DataSet, you may want to set the DataMember property to the name of the table you want to use.

Once this is done, we can add Binding objects to each of our Form’s control’s DataBindings collection. To create a Binding, we generally specify three arguments: a string representing the property to bind to, an object representing the data object (our BindingSource instance), and a string representing the column/property of the data object to bind to.

To demonstrate this, I’ll use a simple class representing a person. The class is called Person and has the properties Firstname, Lastname, Dob and Gender:

  1. class Person
  2. {
  3.     private string firstname;
  5.     public string Firstname
  6.     {
  7.         get { return firstname; }
  8.         set { firstname = value; }
  9.     }
  11.     private string lastname;
  13.     public string Lastname
  14.     {
  15.         get { return lastname; }
  16.         set { lastname = value; }
  17.     }
  19.     private DateTime dob;
  21.     public DateTime Dob
  22.     {
  23.         get { return dob; }
  24.         set { dob = value; }
  25.     }
  27.     private string gender;
  29.     public string Gender
  30.     {
  31.         get { return gender; }
  32.         set { gender = value; }
  33.     }
  35.     public Person(string _firstname, string _lastname,
  36.         DateTime _dob, string _gender)
  37.     {
  38.         firstname = _firstname;
  39.         lastname = _lastname;
  40.         dob = _dob;
  41.         gender = _gender;
  42.     }
  43. }

I’m simply going to create a generic list of Person objects and populate it. The generic list will be our data object:

  1. List<Person> people;
  2. people = new List<Person>();
  3. people.Add(new Person(
  4.     "James", "Bishop",
  5.     new DateTime(1983, 7, 18), "Male"));
  6. people.Add(new Person(
  7.     "Harry", "Smith",
  8.     new DateTime(1971, 2, 13), "Male"));
  9. people.Add(new Person(
  10.     "Jane", "Henshaw",
  11.     new DateTime(1941, 1, 26), "Female"));
  12. people.Add(new Person(
  13.     "Wendy", "Richards",
  14.     new DateTime(1975, 3, 2), "Female"));

To use our data object with our binding source, we simply assign it to the BindingSource’s DataSource property:

  1. BindingSource bs = new BindingSource();
  2. bs.DataSource = people;

Finally, we need to add to our controls’ DataBindings collections. I’ve designed a simple form using three TextBoxes to display Firstname, Lastname and Gender, and I’ve used a MonthCalendar for Dob. To add bindings to them you can do the following:

  1. txtFirstName.DataBindings.Add(
  2.     new Binding("Text", bs, "Firstname"));
  3. txtLastName.DataBindings.Add(
  4.     new Binding("Text", bs, "Lastname"));
  5. calDob.DataBindings.Add(
  6.     new Binding("SelectionStart", bs, "Dob"));
  7. calDob.DataBindings.Add(
  8.     new Binding("SelectionEnd", bs, "Dob"));
  9. txtGender.DataBindings.Add(
  10.     new Binding("Text", bs, "Gender"));

Note the two calls to calDob.DataBindings.Add, to represent the SelectionStart and SelectionEnd properties. The basic form I created looks like this:

A Data Bound Form

A Data Bound Form

Now we have set up our bindings, we need to be able to navigate through the records. The BindingSource class provides methods to do this such as MoveFirst, MoveLast, MoveNext and MovePrevious. It’s fairly easy to wire these to say, Button Click event handlers, but we can use another class to do this wiring for us. This class is the BindingNavigator, which essentially looks like a ToolStrip with buttons to navigate through records. It’s shown above in the View People Form. Simply add one to your form and set its BindingSource property to your BindingSource instance. Clicking the navigation buttons automatically invokes the Move methods of the BindingSource.

The BindingSource is a fairly simple class to use once you understand its purpose. Together with the BindingNavigator, it makes the development of simple data forms a breeze.

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • email
  • LinkedIn
  • Technorati

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.