Udostępnij za pośrednictwem


Walkthrough: Displaying Data on a Form in a Windows Application

One of the most common scenarios in application development is to display data on a form in a Windows-based application. You can display data on a form by dragging items from the Data Sources Window onto the form. This walkthrough creates a simple form that displays data from a single table in several individual controls. This example uses the Customers table from the Northwind sample database.

Tasks illustrated in this walkthrough include:

Prerequisites

In order to complete this walkthrough, you need:

Creating the Windows Application

The first step is to create a Windows Application project.

To create the new Windows Application project

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

  2. Name the project DisplayingDataonaWindowsForm.

  3. Select Windows Application and click OK. For more information, see Creating Windows-Based Applications.

    The DisplayingDataonaWindowsForm project is created and added to Solution Explorer.

Creating the Data Source

This step creates a data source using the Data Source Configuration Wizard based on the Customers table in the Northwind sample database. You must have access to the Northwind sample database to create the connection. For information on setting up the Northwind sample database, see How to: Install Sample Databases.

To create the data source

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

  2. In the Data Sources window, select Add New Data Source 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. For more information, see Add/Modify Connection Dialog Box (General).

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

  6. Click Next on the Save connection string to the Application Configuration file page.

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

  8. Select the Customers table, and then click Finish.

    The NorthwindDataSet is added to your project and the Customers table appears in the Data Sources window.

Setting the Controls to be Created

For this walkthrough the data will be in a Details layout where data is displayed in individual controls. (The alternative approach is the default Grid layout where the data is displayed in a DataGridView control.)

To set the drop type for the items in the Data Sources window

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

  2. Change the drop type of the Customers table to Details by selecting Details from the drop-down list on the Customers node. For more information, see How to: Set the Control to be Created when Dragging from the Data Sources Window.

  3. Change the CustomerID column's drop type to a label by selecting Label from the control list on the CustomerID node.

Creating the Form

Create the data-bound controls by dragging items from the Data Sources window onto your form.

To create data-bound controls on the form

Testing the Application

To run the application

Next Steps

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

See Also

Concepts

What's New in Data

Displaying Data Overview

Data Sources Overview

TableAdapter Overview

Other Resources

Data Walkthroughs