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 Forms 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.
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.
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:
- Access to the Northwind sample database. For more information, see How to: Install Sample Databases.
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 Visual Studio Settings.
To create the project
From the File menu, create a new project.
Select Windows Forms Application and name it DataWalkthrough. For more information, see Creating Windows-Based Applications.
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
On the Data menu, click Show Data Sources.
In the Data Sources window, click the Add New Data Source button in the toolbar 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.
-or-
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 the Connection String to the Application Configuration File page.
Expand the Tables node on the Choose your Database Objects page.
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)
In the Data Sources window, select the Customers table, and then click the drop-down arrow.
Select Details from the Customer table's control list.
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
Press F5.
Select different customers to verify that the correct orders are displayed in the grid on the form.
Modify one or more records.
Click the Save button (the disk icon).
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:
Editing the query that loads data into the application with the TableAdapter wizards. For more information, see How to: Edit TableAdapter Queries, and How to: Edit TableAdapters.
Adding validation to verify the correct data is being entered before sending changes to the database. For more information, see Validating Data.
See Also
Concepts
What's New in Data Application Development
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
Other Resources
Overview of Data Applications in Visual Studio
Connecting to Data in Visual Studio