Walkthrough: Displaying Data on a Windows Form
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:
Creating a new Windows Application project.
Creating and configuring a dataset with the Data Source Configuration Wizard.
Selecting the control to be created on the form when dragging items from the Data Sources window. For more information, see How to: Set the Control to be Created when Dragging from the Data Sources Window.
Creating a data-bound control by dragging items from the Data Sources window onto your form.
In order to complete this walkthrough, you need:
Access to the Northwind sample database. For more information, see How to: Install Sample Databases.
The first step is to create a Windows Application project.
To create the new Windows Application project
From the File menu, create a new project.
Name the project DisplayingDataonaWindowsForm.
Select Windows Application and click OK. For more information, see Creating Windows-Based Applications.
The DisplayingDataonaWindowsForm project is created and added to Solution Explorer.
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
On the Data menu, click Show Data Sources.
In the Data Sources window, select Add New Data Source to start the Data Source Configuration Wizard.
Select Database on the Choose a Data Source Type page, and then click Next.
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.
Select New Connection to launch the Add/Modify Connection dialog box. For more information, see Add/Modify Connection Dialog Box (General).
If your database requires a password, select the option to include sensitive data, and then click Next.
Click Next on the Save connection string to the Application Configuration file page.
Expand the Tables node on the Choose your Database Objects page.
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.
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
Expand the Customers node in the Data Sources window.
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.
Change the CustomerID column's drop type to a label by selecting Label from the control list on the CustomerID node.
Create the data-bound controls by dragging items from the Data Sources window onto your form.
To create data-bound controls on the form
Drag the main Customers node from the Data Sources window onto the form.
Data-bound controls with descriptive labels appear on the form, along with a tool strip (BindingNavigator) for navigating records. A NorthwindDataSet, CustomersTableAdapter, BindingSource, and BindingNavigator appear in the component tray.
To run the application
Navigate records using the BindingNavigator control.
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:
Adding search functionality to the form. For more information, see How to: Add a Parameterized Query to a Windows Forms Application.
Add functionality to send updates back to the database. For more information, see Walkthrough: Saving Data to a Database (Single Table).
Adding the Orders table to the dataset by selecting Configure DataSet with Wizard from within the Data Sources window. Then you can add controls that display related data by dragging the Orders node (the one below the Fax column within the Customers table) onto the form. For more information, see How to: Display Related Data in a Windows Forms Application.