Introducing Data Binding With Windows Form

This article shows how to bind data from a Data Source (such as ADO.NET) to a Windows Forms form.
 
Objectives
  • Learn to Build a simple data-bound form
  • Learn to implement complex data binding
Contents
  • Simple data binding with a TextBox control
  • Complex Data binding with DataGridView and ComboBox
  • Navigating Between records with Binding Navigator
Windows Forms allow you to bind easily to nearly any structure that contains data. With data binding, you do not need to explicitly write the code that instantiates a connection and creates a dataset (as we saw in my last article. http://www.c-sharpcorner.com/UploadFile/e95fe7/introduction-to-ado-net. On the basis of the number of bound values that can be displayed through a control of a windows form, binding can be classified into:
  • Simple data binding
  • Complex data binding

Simple data binding

 
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.
 
DatBinding1.jpg
 
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.
     
    DatBinding2.jpg
     
  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:
     
    DatBinding3.jpg
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.
     
    DatBinding4.jpg
     
  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.
     
    DatBinding5.jpg
     
  5. Execute the Windows Forms form and verify the output. The Employee Details will be displayed as shown in the following figure.
     
    DatBinding6.jpg
The following table describes the various symbols and their function in the BindingNavigator control:
 
DatBinding7.jpg
 

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.
 
Let's see complex data binding with a DataGridView:
  1. Drag and drop a DataGridView from the Toolbox under the Data tab.
     
    DatBinding8.jpg
     
  2. Click on the DataGridView task pop-up menu as shown in the following figure.
     
    DatBinding9.jpg
     
  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.
     
    DatBinding10.jpg
     
  4. In the database configuration wizard select database and click on "Next" as shown in the following figure.
     
    DatBinding11.jpg
     
  5. After clicking on the Next button you will get the following output.
     
    DatBinding12.jpg
     
  6. Click on "New Connection" to create a new connection to your data source.
     
    To add a connection:
     
    • Provide the server name (in my case it is (.))
    • If your server is not using Windows authentication then select "Use SQL Server authentication".
    • Provide the username and password.
    • Provide the database name AdventureWorks from the Select or enter a database name drop-down list.
    • Click on the Test Connection Button. If everything goes well you will see a message box saying thatTest the connection succeeded as shown in the following figure.
     
    DatBinding13.jpg
     
  7. Click the OK button.
  8. Click the OK button on the Add Connection dialog box.
  9. Select Yes, Include sensitive data in the connection string, and click the "Next" button in the DataSource configuration wizard. You will get a page as displayed in the following figure.
     
    DatBinding14.jpg
     
  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.
     
    DatBinding15.jpg
     
  12. Expand the table node and select the HumanResources.Employee table is shown in the following figure.
     
    DatBinding16.jpg
     
  13. Click the "Finish" button. The form is displayed, as shown in the following figure.
     
    DatBinding17.jpg
     
  14. Press F5 to execute the application. You will get the following output.
     
    DatBinding18.jpg

Summary

 
In this article, you learned Data Binding is the ability to bind some elements of a data source with the controls of Windows Forms to form without writing code. You also learned that there are two types of data binding as in the following:
  1. Simple Data binding: the process of binding a control such as a TextBox or a label.
  2. Complex data binding: the process of binding a control such as ComboBox, DataGridView, or ListBox.
In the next article, we will see how to filter records in ADO.NET.


Similar Articles