Walkthrough: Filling a Dataset with Data
This walkthrough creates a dataset with one data table and fills it with data from the Customers table in the Northwind sample database. The dataset is filled with data by calling the Fill method of a TableAdapter.
During this walkthrough, you will learn how to:
In order to complete this walkthrough, you will 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.
To create the new Windows project
In Visual Studio, from the File menu, create a new Project.
Name the project DatasetWalkthrough.
Select Windows Application and click OK. For more information, see Creating Windows-Based Applications.
The DatasetWalkthrough project is created and added to Solution Explorer.
This step creates a dataset 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 dataset
On the Data menu, click Show Data Sources.
In the Data Sources window, click 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.
You can create data-bound controls by dragging items from the Data Sources window onto a form in your Windows application.
To create a DataGridView bound to the Customers table
Dragging items from the Data Sources window onto a form automatically adds the correct code to fill the dataset into the Form1_Load event handler.
To load data into a dataset
In Solution Explorer, select Form1, and click the View Code button.
Inspect the Form1_Load event handler. The TableAdapter's Fill method fills the dataset with data.
You can optionally copy this code to other parts of your application where you need to fill the dataset.
To run the application
Press F5 to run the application.
The dataset is populated with data and is displayed in the DataGridView.
Depending on your application requirements, there are several steps you may want to perform after creating a data-bound 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.
Adding 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. Now 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.