Export (0) Print
Expand All

Data Binding and Windows Forms

Visual Studio .NET 2003

In Windows Forms, you can bind to not just traditional data sources, but also to almost any structure that contains data. How the data gets into the structure is not important; thus, 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.

Further, you can bind any property of any control to the data source. In traditional data binding, you typically bind the display property (for example, the Text property of a TextBox control) to the data source. With the .NET Framework, you also have the option of setting other properties via binding as well. Some examples of what you might use binding for include:

  • Setting the graphic of an image control.
  • Setting the background color of one or more controls.
  • Setting the size of controls.

In other words, data binding becomes an automatic way of setting any run-time accessible property of any control on the 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.

Simple Data Binding
Simple data binding is 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 or Label control — that is, a control that typically only displays a single value. In fact, any property on a control can be bound to a field in a database. For more information on simple data binding, see Creating a Simple-Bound Control on a Windows Form.
Complex Data Binding
Complex data binding is the ability of a control to bind to more than one data element, typically more than one record in a database, or to more than one of any other type of bindable data element. Examples of controls that support complex binding are the DataGrid, ListBox, and ErrorProvider controls.

Common Scenarios That Employ Data Binding

In taking a view of the entire body of Windows applications, you will discover that nearly every commercial application uses information read from data sources of one sort or another, often by way of data binding. Below is a rundown of a few of the most common scenarios that utilize data binding as the method of data presentation and manipulation.

Reporting
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 it is always wisest to choose the layout that best suits the data.
Data Entry
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.
Master/Detail Relationship
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 on creating a master/detail application with two Windows Forms DataGrid controls, see Creating Master-Details Lists with the Windows Forms DataGrid Control.
Lookup Table
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 had 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. For more information on creating an application featuring a table lookup, see Creating a Lookup Table for a Windows Forms ComboBox, ListBox, or CheckedListBox Control.

See Also

Windows Forms Data Architecture | Binding the Windows Forms DataGrid Control to a DataSet | Binding Class | Data Form Wizard

Show:
© 2014 Microsoft