Lab 1 Creating a Data Bound Windows Form

In this lab you will create a Windows Form with controls bound to data.

► Exercise 1: Create a Data-Bound Form with the Data Sources Wizard

Let's now create a Windows application and demonstrate creating a data source and binding controls to data by dragging and dropping items from the Data Sources window.

1. Create a Windows application and name it DataSourcesWizardExample.

2. Start the Data Source Configuration Wizard by selecting Add New Data Source from the Data menu.

3. On the Choose a Data Source Type page, leave the default selection of Database and click Next.

4. On the Choose Your Data Connection page, select a connection to the North-wind sample database or create a new connection if needed.

5. Click Next, keeping the default values until you get to the Choose Your Database Objects page, and select the following from the Tables node:

□ Order Details

6. Click Finish to add the dataset to your project.

7. On the Data menu, select Show Data Sources to display the Data Sources window. The Data Sources window should appear similar to Figure 8-1.

B£E3nSB

NorthwindDataSet

i abl CustomerlD

abl CompanyName abl ContactName abl ContactTitle abl Address abl City abl Region abl PostalCode abl Country abl Phone abl Fax + _l Orders J Order Details _] Orders

Figure 8-1 Data Sources window displaying the Customers, Orders, and Order Details tables from the NorthwindDataSet

8. Drag the Customers node from the Data Sources window onto Form1.

A DataGridView and BindingNavigator are added to the form, and several data-related objects appear in the component tray, as shown in Figure 8-2.

Figure 8-2 Form1 and additional components after dragging Customers node from the Data Sources window

9. Run the application.

At this point, you have a working application with a DataGridView data-bound to the Customers table. If you switch to code view in the Integrated Development Environment (IDE), you can see that code has been added to fill the Customers table with data in the form load event, and code has been added to the save button of the BindingNavigator to send updates back to the database.

The running application should display the Customers data as shown in Figure 8-3.

aHB ^iaJisJ

Customer! D | CompanyName

ContactName

Cc-t

ALFKI

Alfreds Futterkiste

Maria Anders

Sa—

ANATR

Ana Trujillo Empa...

Ana Trujillo

0^

ANTON

Antonio Moreno...

Antonio Moreno

0*

AROUT

Around the Horn

Thomas Hardy

Sa

BERGS

Berglunds snabb...

Christina Berglund

Ort

BLAUS

Blauer See Delik...

H anna Moos

Sa

BLONP

Blondesddsl père...

Frédérique Citeaux

Me

BOLID

Bólido Comidas p...

Martin Sommer

0*

BO NAP

Bon app'

Laurence Lebihan

Ov-

j_T

hi i

Figure 8-3 Form displaying data from the Customers table

Figure 8-3 Form displaying data from the Customers table

10. Stop the application and open the form in Design view.

11. Expand the Customers node in the Data Sources window.

12. Drag the Orders node (nested within the Customers node) onto the form.

Notice the OrdersBindingSource and OrdersTableAdapter added to the component tray.

13. Run the application.

Click a row in the Customers table. Notice that the Orders DataGridView displays all orders for the selected customer, as shown in Figure 8-4.

jnjxl

4 i

77 of 91

i ► h i # x y

Customed D

CompanjiName

ContactName —

SIMOB

Simons bistro

Jytte Peteisen

SPECD

Spécialités du monde

Dominique Periier

SPUR

Split Rail Beer & Ale

Art Braunschweiger

SUPRD

Suprêmes délices

Pascale Cartrain

THEBI

The Big Cheese

Liz Nixon

THECR

The Crackei Box

Liu Wong

TOMSP

Toms Spezialitäten

Karin Josephs

TORTU

Toituga Restauiante

Miguel Angel Pao...—'

TRAD H

Tradiçâo Hipermeicados

Anabela Doming... ,

Z-

M 1

-tr

□rdeilD

CustomerlD

Employee! D

Order

10310

THEBI

B

9/20/'

10708

THEBI

B

10/17

10805

THEBI

2

12/30

10992

THEBI

1

4/1/1!

*

hi i m

Figure 8-4 Form displaying Orders data for the selected customer

Was this article helpful?

0 0

Post a comment