Simple Data Binding in Windows Forms

for RuBoard

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

  1. Create a new project in VS .NET by choosing File, New, and then choosing the Project option.

  2. 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.

  1. 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).

    Figure 9.1. Visual Studio .NET Form with Data Toolbox SqlDataAdapter object selected.

    graphics/09fig01.jpg

    This will automatically invoke the Data Adapter Configuration Wizard. Both the data connection and the data adapter can be fully configured here.

    1. 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).

    2. 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.

    3. 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:

       SELECT * FROM Customers 
    4. The wizard will show you the tasks that it has done and indicate whether the SqlDataAdapter has been configured successfully.

  2. 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 .

    1. 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.

      Figure 9.2. Generate a new dataset for the form.

      graphics/09fig02.jpg

    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.

    3. 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).

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):

  • Textbox With a name of txtCustomerID and text is blank.

  • Textbox With a name of txtCompanyName and text is blank.

  • Textbox With a name of txtContactName and text is blank.

  • Button With 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.

Figure 9.3. Add text boxes, button, and labels to the form.

graphics/09fig03.jpg

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.4. Adding code for the Button method.

graphics/09fig04.jpg

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.

  1. From the Forms Designer, select the txtCustomerID text box and press F4. This will position you to the properties window for this text box.

  2. Expand the (DataBindings) node in the properties list and its text property.

  3. 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).

  4. Now, from the Forms Designer, select the txtCompanyName text box and press F4.

  5. Expand the (DataBindings) node in the properties list and its text property.

  6. Within the text property, expand the DataSet1 and Customers nodes and select the CompanyName column from the list.

  7. Finally, from the Forms Designer, select the txtContactName text box and press F4.

  8. Expand the (DataBindings) node in the properties list and its text property.

  9. 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.

graphics/09fig05.jpg

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:

  • DataSource An object that must implement the Ilist interface such as a DataSet or an array

    • IList inherits from

      • Icollection A set of objects of similar type

      • Ienumerable Provides an enumerator, which allows you to traverse a collection one item at a time (via the MoveNext() method).

  • DisplayMember/DataMember allows 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.

for RuBoard


Sams Teach Yourself ADO. NET in 24 Hours
Sams Teach Yourself ADO.NET in 24 Hours
ISBN: 0672323834
EAN: 2147483647
Year: 2002
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net