Exp 28 29 30 Doc
Exp 28 29 30 Doc
Simple data binding allows you to bind a control to a single data element. The most
common use of simple data binding involves binding a single data element, such as the
value of a column in a table, to a control on a form. You use this type of data binding for
controls that show only one value. Uses of simple data binding include binding data to
text boxes and labels. Consider a scenario where a Windows Forms form needs to be
created to display employee details in the following way.
In the preceding form, one value needs to be displayed in each of the controls.
Therefore simple data binding will be performed for each control.
Steps
1. Press F4 to open the properties window.
2. Select the first TextBox to display its properties window.
3. Expand the (DataBindings) property.
4. Select the text property to enable the drop-down list. Click the drop-down list.
5. Add a project data source in the drop-down list.
6. Create a connection with the AdventureWorks database and select the
HumanResources.employee table.
7. Select the first TextBox. Expand "Other data source" ---> "Project data source" -->
"AdventureWorksdataset" --> "Employee" --> "EmloyeeId".
8. Select the second TextBox. Expand the DataBinding property then select "Text" ---
> "Employee Binding source" then select column(National ID) from the list.
9. Similarly, bind TextBox3 and TextBox4 with the column contactid and Login ID.
10. Press F5. If everything goes well, you will see the following output:
Here is one problem with the preceding. Every time you run your project, you are able to
see only one record. So how to solve this problem?
We can solve that problem using the BindingNavigator Control. For every data source
that is bound to a Windows Forms control, there exists a BindingNavigator control. The
BindingNavigator control handles the binding to the data by keeping the pointer to the
item in the record list current.
Implementing BindingNavigator
1. Drag and drop a BindingNavigator control from the Toolbox.
2. Select BindingNavigator1; this will display the properties window.
3. Select the bindingSource property from the properties Window to enable the
corresponding drop-down list.
4. Select employeeBindingSource from the Drop-Down list as shown in the following
figure.
5. Execute the Windows Forms form and verify the output. The Employee Details will
be displayed as shown in the following figure.
The following table describes the various symbols and their function in the
BindingNavigator control:
Complex Data Binding
Complex data binding allows you to bind more than one data element to control. Using
the column example, complex data binding involves binding more than one column or
row from the underlying record source. Controls that support complex data binding
include data grid controls, combo boxes, and list boxes.
2. Click on the DataGridView task pop-up menu as shown in the following figure.
3. Select the choose Data Source drop-down list and then select the Add Project
Data Source from the DataGridView task pop-up menu as shown in the following
figure.
4. In the database configuration wizard select database and click on "Next" as shown
in the following figure.
5. After clicking on the Next button you will get the following output.
To add a connection:
10. Ensure that the Yes, save the connection as the check box is selected and the
AdventureWorksConnection string appears in the TextBox.
Note: We will see what the use of saving the connection string in the App.config file
is in my future article on ADO.Net.
11. Click the "Next" button. The Choose Your Database Objects page is displayed as
shown in the following figure.
12. Expand the table node and select the HumanResources.Employee table is shown
in the following figure.
13. Click the "Finish" button. The form is displayed, as shown in the following figure.
14. Press F5 to execute the application. You will get the following output.