- Binding Data to Controls
- Simple Data Binding in Windows Forms
- Complex Data Binding in Windows Forms
- Summary
- Q&A
- Workshop
Simple Data Binding in Windows Forms
Let's quickly step through an example that will display a Customer ID, Company Name, and Contact Name from the Customer table in the Northwind database supplied in Microsoft SQL Server. More specifically, you will be creating a few TextBox controls on a form and binding the Text property on these TextBox controls to the Customers data source. Then, you will create a button that will activate the data retrieval and display the data to the form. This is simple binding.
Create a New Project in VS .NET
Create a new project in VS .NET by choosing File, New, and then choosing the Project option.
When the New Project dialog box appears, choose Visual Basic Projects (or Visual C# Projects) and Windows Applications. Name this project "ADO.NET24hoursDB." This creates a default form for you to start from.
Add the Data Connection, Data Adapter, and DataSet
We will be accessing the Customers table in SQL Server's Northwind database. So, first we will need to create a data connection and a data adapter to Microsoft SQL Server.
-
From the Data tab of the Toolbox, drag a SQLDataAdapter object into your form as shown in Figure 9.1 (or OleDBDataAdapter if you want).
-
The wizard starts with the Choose Your Data Connection dialog box. If you already have a connection defined in your project, it will be placed in the dialog box. Otherwise, choose to create a new connection and specify the appropriate connection information (test the connection as well).
-
You will then have to decide to supply SQL statements, build a new stored procedure, or give the name of an existing stored procedure for the data access. In our example we will use the Use SQL Statements option.
-
You will be presented with a Generate the SQL Statements dialog box where you will simply type in a valid SQL statement, or you can use the Query Builder option to formulate the SQL query. For our example, just type in the following query:
-
The wizard will show you the tasks that it has done and indicate whether the SqlDataAdapter has been configured successfully.
-
After the SqlDataAdapter and DataConnection objects have been configured and added to the form, you must generate a DataSet and then add an instance of this DataSet to the form. We will be binding our TextBox properties to the columns in the DataSet.
-
Simply right-click on the SqlDataAdapter (SqlDataAdapter1) that is on your form and choose the Generate Dataset menu option as seen in Figure 9.2.
-
Now, just choose to create a new DataSet using the default name that it provides (DataSet1). Make sure you have checked the Customers table and checked the box for it to be added to the designer.
-
When the process finishes, a DataSet instance named DataSet11 will be on the form and a dataset schema will be in the Solutions Explorer (named DataSet1.xsd).
This will automatically invoke the Data Adapter Configuration Wizard. Both the data connection and the data adapter can be fully configured here.
Figure 9.1 Visual Studio .NET Form with Data Toolbox SqlDataAdapter object selected.
SELECT * FROM Customers
Figure 9.2 Generate a new dataset for the form.
Create Text Boxes, Labels, and Buttons
The next step is to complete the form example to include a few text boxes and a control button. From the Windows Forms tab of the Toolbox, add the following (drag and drop on the form):
-
TextboxWith a name of txtCustomerID and text is blank.
-
TextboxWith a name of txtCompanyName and text is blank.
-
TextboxWith a name of txtContactName and text is blank.
-
ButtonWith a name of btnGetCustomer and text of "Get Customer".
Go ahead and add labels in front of each text box so that it looks like the form in Figure 9.3.
Add Code to Populate the DataSet
Now we are ready to complete the application by adding the code to fill the DataSet.
Just double-click on the Get Customer button to create a method for the Click event. You will have to add code to make a call to the DataSet's Clear method to clear the DataSet out between iterations, and make a call to the data adapter's Fill method to get data from the database (as you can see in Figure 9.4). The following code is added:
Customers1.Clear() SqlDataAdapter1.Fill(Customers1)
Figure 9.3 Add text boxes, button, and labels to the form.
Figure 9.4 Adding code for the Button method.
Bind the Text Boxes to the DataSet
Nothing is left to do other than bind (simple binding) the text boxes to the columns in the DataSet and run the application.
-
From the Forms Designer, select the txtCustomerID text box and press F4. This will position you to the properties window for this text box.
-
Expand the (DataBindings) node in the properties list and its text property.
-
Within the text property, expand DataSet1 and Customers nodes and select the CustomerID column from the list (look back at Figure 9.3 in the lower right corner to see the (DataBinding) property).
-
Now, from the Forms Designer, select the txtCompanyName text box and press F4.
-
Expand the (DataBindings) node in the properties list and its text property.
-
Within the text property, expand the DataSet1 and Customers nodes and select the CompanyName column from the list.
-
Finally, from the Forms Designer, select the txtContactName text box and press F4.
-
Expand the (DataBindings) node in the properties list and its text property.
-
Within the text property, expand the DataSet1 and Customers nodes and select the ContactName column from the list.
Test It!
That's it! Now just hit the F5 key and test your application by clicking on the Get Customer button. It will put the first customer's information (CustomerID, CompanyName, and ContactName) that it finds in the DataSet into the appropriate text boxes. In Figure 9.5, you can see the form displaying a customer's information successfully.
Figure 9.5 The Get Customer Forms application, showing simple binding.
The following snippet of code is from the forms1.vb program for this example and shows the explicit data binding for each text box. When you are using the Forms Designer, much of this code is generated for you (as you specify the data binding properties):
Me.txtCustomerID.DataBindings.Add(New System.Windows.Forms.Binding ("Text", Me.DataSet11, "Customers.CustomerID")) Me.txtCompanyName.DataBindings.Add(New System.Windows.Forms.Binding ("Text", Me.DataSet11, "Customers.CompanyName")) Me.txtContactName.DataBindings.Add(New System.Windows.Forms.Binding ("Text", Me.DataSet11, "Customers.ContactName"))
List Controls and DataGrid Controls
These are the key properties for list controls and DataGrid controls:
-
DataSourceAn object that must implement the Ilist interface such as a DataSet or an array
-
IList inherits from
IcollectionA set of objects of similar type
-
IenumerableProvides an enumerator, which allows you to traverse a collection one item at a time (via the MoveNext() method).
-
DisplayMember/DataMemberallows you to specify a table or elements within a table.
Note: The classes and interfaces in the System.Web.UI.WebControls namespace are similar and equivalent.