Data Binding and Windows Forms
In Windows Forms, you can bind to not just traditional data sources, but also to almost any structure that contains data. You can bind to an array of values that you calculate at run time, read from a file, or derive from the values of other controls.
In addition, you can bind any property of any control to the data source. In traditional data binding, you typically bind the display property—for example, theproperty of a control—to the data source. With the .NET Framework, you also have the option of setting other properties through binding as well. You might use binding to perform the following tasks:
Setting the graphic of an image control.
Setting the background color of one or more controls.
Setting the size of controls.
Essentially, data binding is an automatic way of setting any run-time accessible property of any control on a form.
Types of Data Binding
Windows Forms can take advantage of two types of data binding: simple binding and complex binding. Each offers different advantages.
|Type of data binding||Description|
Simple data binding
The ability of a control to bind to a single data element, such as a value in a column in a dataset table. This is the type of binding typical for controls such as a TextBox control orcontrol, which are controls that typically only displays a single value. In fact, any property on a control can be bound to a field in a database. There is extensive support for this feature in Visual Studio.
Interfaces Related to Data Binding
Complex data binding
The ability of a control to bind to more than one data element, typically more than one record in a database. Complex binding is also called list-based binding. Examples of controls that support complex binding are the, , and controls. For an example of complex data binding, see .
To simplify data binding, Windows Forms enables you to bind a data source to thecomponent and then bind controls to the BindingSource. You can use the BindingSource in simple or complex binding scenarios. In either case, the BindingSource acts as an intermediary between the data source and bound controls providing change notification currency management and other services.
Common Scenarios That Employ Data Binding
Nearly every commercial application uses information read from data sources of one type or another, usually through data binding. The following list shows a few of the most common scenarios that utilize data binding as the method of data presentation and manipulation.
Reports provide a flexible way for you to display and summarize your data in a printed document. It is very common to create a report that prints selected contents of a data source either to the screen or to a printer. Common reports include lists, invoices, and summaries. Items are usually formatted into columns of lists, with sub-items organized under each list item, but you should choose the layout that best suits the data.
A common way to enter large amounts of related data or to prompt users for information is through a data entry form. Users can enter information or select choices using text boxes, option buttons, drop-down lists, and check boxes. Information is then submitted and stored in a database, whose structure is based on the information entered.
A master/detail application is one format for looking at related data. Specifically, there are two tables of data with a relation connecting them—in the classic business example, a "Customers" table and an "Orders" table with a relationship between them linking customers and their respective orders. For more information about creating a master/detail application with two Windows Forms DataGridView controls, see
Another common data presentation/manipulation scenario is the table lookup. Often, as part of a larger data display, a ComboBox control is used to display and manipulate data. The key is that the data displayed in the ComboBox control is different than the data written to the database. For example, if you have a ComboBox control displaying the items available from a grocery store, you would probably like to see the names of the products (bread, milk, eggs). However, to ease information retrieval within the database and for database normalization, you would probably store the information for the specific items of a given order as item numbers (#501, #603, and so on). Thus, there is an implicit connection between the "friendly name" of the grocery item in the ComboBox control on your form and the related item number that is present in an order. This is the essence of a table lookup.
How to: Create a Lookup Table with the Windows Forms BindingSource Component