With the DataAdapter Wizard, we have seen how to bind a DataGrid Control to the table. We can also bind other different types of controls to the database. If we want to view all the records at a time, generally we would use a DataGrid control. To view individual records, we normally bind controls like a Textbox to the table.
Types of Controls to Bind
Unlike VB6, data binding has a much broader sense in VB.NET. Any property of any control can be bound to any structure that contains data. This is achieved by using the DataBindings property. This property contains a collection of Binding objects for the control. Traditionally, we typically bind the display property of a control to a data source (e.g. Text property of a Textbox control). But in VB.NET we have the option to bind other properties also. We can bind properties like background/foreground color, size, image etc. for a control to a data source, making for a totally flexible setup.
VB.NET supports different types of User Interfaces and each of them requires different controls. The Web and Windows are the two different User Interfaces supported by VB.NET. Although many of the controls for both of the interfaces look similar, there are some differences in the way that they are used.
Data binding in windows forms
Simple Data Binding: Generally, a control's ability to bind to an element like a value in a table column is thought of as Simple Binding. Binding the Text property of a Textbox to a single value in a column of a table is a good example. The Textbox, Label, ListBox, ComboBox and CheckedListBox controls are all good examples of controls that are frequently bound to data in a windows application.
Complex Data Binding: Data binding is said to be complex when more than one element from a record can be bound to a given control. The DataGrid is a good example of this where we mostly bind entire records to the control.
Data binding in web forms Often web forms use data from database, XML documents, streams or some other data source. Due to the nature of web applications, data access in such applications differs from that of a windows application. The data binding architecture assumes that most of the time, data requirements for a web application are read only. Therefore, web form data architecture does not perform updates. We have to provide the logic ourselves if we want to do that. The logic will largely depend upon the type of the data set as well as the control being used, and the concurrency requirements.
Just like windows forms however, you can bind the controls to any structure that provides data, although normally they are bound to a DataSet control. Some Web Form controls display values from multiple records at once. These controls include the Repeater, DataList, DataGrid, ListBox, CheckBoxList, and RadioButtonList Web server controls, as well as the HtmlSelect control. Other controls, such as the Label, TextBox, CheckBox, and Hyperlink controls display single values, which in data-binding terms means values from a single record.
Types of properties to bind
We have already seen that VB.NET allows almost all properties of a control to be bound to a data source. Let us see how we can bind these properties to controls right now.
We can create simple data bound controls using the following steps:
1. Start a new Windows application. Connect to a data source. Creating a DataAdapter and generating a DataSet will be one of the quickest ways to start.
2. Design the form, laying out the controls to be bound to data. Select a control to be bound (say a Textbox) and display the properties.
3. Expand the DataBindings property of the selected control. Properties most frequently bound for that control are displayed in this collection. (For a Textbox, the Text property will be displayed here).
4. Expand the list of available data sources for that property. It will display the data sources available for binding. Select the one you want to use. (Say the DataSet created in the last example. Suppose, your DataSet contains records from Authors table, Select the DataSet and expand it. It will show the list of fields available for binding. Select the field you want to bind to the control.
5. If the property to be bound is not found under the DataBindings, then we can click the Ellipsis (...) button to display the advanced data binding properties. This opens up an Advanced Data Binding Dialogue Box that displays all the properties. We can select the required property and data source to be connected from the dialog box.
For controls like the ComboBox, ListBox etc, their DisplayMember property is normally bound to the data source.
Since simple-bound data controls show only one value from a column at a time, it is typical to write navigation logic for such controls. Complex Data Binding is the ability of a control to bind to more than one single element, typically more than one record at a time. The DataGrid and ListBox controls are good examples of this. We have bound a DataGrid control to a data source when we used the DataAdapter configuration wizard earlier.