Using Visual Studio 2005’s BindingNavigator, and BindingSource Controls with Custom Collections.

Author: Terry Voss

 

Introducing the Controls:

Microsoft Definitions: The BindingNavigator control is a standardized means to enable simple navigation and manipulation of data on a Windows Form.  The BindingNavigator depends on the BindingSource control. The  BindingSource control encapsulates the datasource for a form and simplifies binding controls on a form to data by providing a layer of indirection, providing currency management, change notification and other services.

These 2 controls allow binding of data to Windows Form controls without the need to write a lot of code. In many practical situations it is possible to write no code manually and produce a form with ample functionality. Many walkthrough examples of this exist as in the following link attests: Display Related Data on a Windows Form

Since many developers see strongly typed collections as an improved alternative to the DataSet or other Classes in some contexts, I will focus on using these controls with one popular third party custom collection so you can easily consider using your collections in this way.

Dragging one BindingNavigator control and one BindingSource control to the Form or the Component Tray causes the Component Tray below the form to look like this. I will explain the third control later.  You will see a navigation type control visible on the form also.

Image 1 – The controls after being dragged onto your form.

 

Setting the Main Properties:

The main property of bindingNavigator1 to set is the BindingSource property. I open the dropdown for the property to see only bindingSource1 listed, so that was easy, and I am immediately prepared to focus my attention on bindingSource1. I open the dropdown for the DataSource property to see a hyperlink: Add Project Data Source

Clicking on this opens a Data Source Configuration Wizard with 3 choices on the first page:

1)     Database

2)     Web Service

3)     Object

I choose object, and then click on Next. I am now prompted to “Select the object you wish to bind to”. There are no objects in the list because by default, the “Hide assemblies that start with Microsoft or System” CheckBox is checked. I click on the “Add References” button and browse to SscDal.dll file. This file holds one collection class for each table in a database because it was generated by an Object Relational Mapper that also maps relations between tables to create associations between the collections.

These are shown to me as a hierarchy of classes now and I pick one collection: PersonCollection

This finishes the wizard, and if you now do not see the Data Sources Window, From the Visual Studio 2005 Data Menu, choose the “Show Data Sources” option to view that window. Here is where you can see the visual data in figure 2.

Image 2 – The result of the Data Configuration Wizard.

 

Getting a Final Result:

I now drag Firstname Property and Lastname Property of the PersonEntity that PersonCollection collects onto my Windows Form. I get 2 TextBox controls with their labels attached. When I drag Adate notice by the icon that I can expect another control than a TextBox. 

What is Recurring? Recurring is an automatically created property of PersonEntity that holds a reference to a collection of Recurring Events that belong to the person. A childlike relation, so when I drag it to the form, I get a DataGridView which causes the third control, recurringBindingSource, on the Component Tray to appear with its main properties already set. 

When I run this form at this stage, things compile and run fine, but I get an empty set of Persons and Grid rows also.

Microsoft’s BindingSource control understands a lot about my Collection, but not enough to instantiate anything but an empty collection. This collection needs a sophisticated method called GetMulti with many overloads to handle 90% of all possible filterings and sortings that Stored Procedures could create. It takes 3 lines of code to remedy this as shown in Listing 1.

We reset the bindingSource1 DataSource property at runtime in the Form Load Event, using the null filter. We could also use complex filter and sorting to get any collection.

Listing 1 – Form_Load Event Code.

 

Now the form runs showing the following data.
 

Image 3 – The running result.

 

I changed the Adate label to Anniversary, but I made no other change. Besides the quickness to get a result with 3 lines of code, the BindingNavigator and BindingSource controls seem to understand many types of data. See if you can get these controls to work with your favorite Collections.