Condividi tramite


Walkthrough: Creating a Simple Data Application

One of the most common scenarios in application development is to display data from a database on a Windows Form. You can display data on forms in Windows applications by dragging items from the Data Sources Window onto your form. This walkthrough demonstrates how to create an application that displays data from two related tables in a database.

For information on creating data applications using WPF, see Binding WPF Controls to Data in Visual Studio.

For information on creating data applications using Silverlight, see Binding Silverlight Controls to Data in Visual Studio.

For information on creating data applications using ASP.NET Web Forms, see ASP.NET Web Forms Data Access Options

This walkthrough illustrates the following tasks:

  • Creating a Windows-based application.

  • Creating and configuring a dataset that is based on the Customers and Orders tables in the Northwind database by using the Data Source Configuration Wizard.

  • Adding controls to display data from the Customers table.

  • Adding controls to display the orders based on the selected customer.

  • Testing the application, selecting different customers and verifying that the correct orders are shown for the selected customer.

  • Modifying data and saving it back to the database.

link to video For a video version of this topic, see Video How to: Creating a Simple Data Application.

Prerequisites

You need the following components to complete this walkthrough:

Creating the Project

The first step is to create a Windows-based application.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

To create the project

  1. From the File menu, create a new project.

  2. Select Windows Forms Application and name it DataWalkthrough. For more information, see Developing Client Applications with the .NET Framework.

  3. Click OK.

    The DataWalkthrough project is created and added to Solution Explorer.

Creating the Data Source

This step creates a dataset that is based on the Customers and Orders tables in the Northwind sample database by using the Data Source Configuration Wizard.

To create the data source

  1. On the Data menu, click Show Data Sources.

  2. In the Data Sources window, click the Add New Data Source button in the toolbar to start the Data Source Configuration Wizard.

  3. Select Database on the Choose a Data Source Type page, and then click Next.

  4. On the Choose Your Data Connection page do one of the following:

    • If a data connection to the Northwind sample database is available in the drop-down list, select it.

      -or-

    • Select New Connection to launch the Add/Modify Connection dialog box.

  5. If your database requires a password, select the option to include sensitive data, and then click Next.

  6. Click Next on the Save the Connection String to the Application Configuration File page.

  7. Expand the Tables node on the Choose your Database Objects page.

  8. Select the Customers and Orders tables, and then click Finish.

    The NorthwindDataSet is added to your project and the Customers and Orders tables appear in the Data Sources window.

Creating Controls to Display Data from the Customers Table

You create data-bound controls by dragging items from the Data Sources window onto a Windows Form.

To create controls to display the customer data (parent records)

  1. In the Data Sources window, select the Customers table, and then click the drop-down arrow.

  2. Select Details from the Customer table's control list.

  3. Drag the main Customers node from the Data Sources window onto Form1.

    Data-bound controls with descriptive labels appear on the form. The following components appear in the component tray:

    • NorthwindDataSet. The typed dataset that contains the Customers and Orders tables.

    • CustomersBindingSource. The BindingSource that binds the controls on the form to the Customers data table in NorthwindDataSet.

    • CustomersBindingNavigator. The BindingNavigator that is used for traversing the records in the Customers table.

    • CustomersTableAdapter. The TableAdapter that communicates between the database and NorthwindDataSet. For more information, see TableAdapter Overview.

    • TableAdapterManager. The TableAdapterManager component that is used to control the order of Inserts, Updates, and Deletes for all TableAdapter components in the dataset. For more information, see TableAdapterManager Overview.

Creating Controls to Display Data from the Orders Table

To create controls to display the orders for each customer (child records)

  • In the Data Sources window, expand the Customers node and select the last column in the Customers table, which is an expandable Orders node beneath the Fax column, and drag it onto the bottom of Form1. (This node in the Customers table hierarchy represents the related orders for a customer, as opposed to the main Orders node, which represents all records in the Orders table and not the orders for an individual customer.)

    A DataGridView is added to the form, and a new BindingSource component (OrdersBindingSource) and TableAdapter (OrdersTableAdapter) are added to the component tray.

    Note

    Open the Properties window and select the OrdersBindingSource component. Inspect the DataSource and DataMember properties to see how data binding is configured to display related records. The DataSource is set to the CustomersBindingSource (the parent table's BindingSource), as opposed to the Orders table. The DataMember property is set to FK_Orders_Customers, which is the name of the DataRelation object that relates the tables to each other.

Testing the Application

To test the application

  1. Press F5.

  2. Select different customers to verify that the correct orders are displayed in the grid on the form.

  3. Modify one or more records.

  4. Click the Save button (the disk icon).

  5. Verify that the changes were saved to the database.

Next Steps

Depending on your application requirements, there are several steps you may want to perform after creating a master-detail form. Some enhancements you could make to this walkthrough include:

See Also

Concepts

Binding Windows Forms Controls to Data in Visual Studio

Preparing Your Application to Receive Data

Fetching Data into Your Application

Binding Controls to Data in Visual Studio

Editing Data in Your Application

Validating Data

Saving Data

TableAdapterManager Overview

Other Resources

Data Walkthroughs

Overview of Data Applications in Visual Studio

Connecting to Data in Visual Studio

Hierarchical Update

Working with Datasets in N-Tier Applications

LINQ to SQL