This article shows how to bind data from a Data Source (such as ADO.NET) to a Windows Forms form.
- Learn to Build a simple data bound form
- Learn to implement complex data binding
- 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.
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.
- Press F4 to open the properties window.
- Select the first TextBox to display its properties window.
- Expand the (DataBindings) property.
- Select the text property to enable the drop-down list. Click the drop-down list.
- Add a project data source in the drop down list.
- Create a connection with the AdventureWorks database and select the HumanResources.employee table.
- Select the first TextBox. Expand "Other data source" ---> "Project data source" --> "AdventureWorksdataset" --> "Employee" --> "EmloyeeId".
- Select the second TextBox. Expand the DataBinding property then select "Text" ---> "Employee Binding source" then select column(National ID) from the list.
- Similarly bind TextBox3 and TextBox4 with the column contactid and Login ID.
- 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 thart 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.
- Drag and drop a BindingNavigator control from the Toolbox.
- Select BindingNavigator1; this will display the properties window.
- Select the bindingSource property from the properties Window to enable the corresponding drop-down list.
- Select employeeBindingSource from the Drop-Down list as shown in the following figure.
- 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 a 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:
- Drag and drop a DataGridView from the Toolbox under the Data tab.
- Click on the DataGridView task pop-up menu as shown in the following figure.
- 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.
- In the database configuration wizard select database and click on "Next" as shown in the following figure.
- After clicking on the next button you will get the following output.
- 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.
- Click the OK button.
- Click the OK button on Add Connection dialog box.
- 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.
- Ensure that the Yes, save the connection as 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 App.config file is in my future article on ADO.Net.
- Click the "Next" button. The Choose Your Database Objects page is displayed as shown in following figure.
- Expand the table node and select the HumanResources.Employee table as shown in the following figure.
- Click the "Finish" button. The form is displayed, as shown in the following figure.
- Press F5 to execute the application. You will get the following output.
In this article you learned Data Binding is the ability to bind some elements of a data source with the controls of a Windows Forms form without writing code. You also learned that there are two types of data binding as in the following:
- Simple Data binding: the process of binding a control such as TextBox or a lable.
- 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.