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:
Create a new Windows Application.
Create and configure a dataset with the Data Source Configuration Wizard.
Create a data-bound DataGridView by dragging from the Data Sources window onto a form.
Prerequisites
In order to complete this walkthrough, you will need:
- Access to the Northwind sample database. For more information, see How to: Install Sample Databases.
Creating a Windows Application
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 Developing Client Applications with the .NET Framework.
The DatasetWalkthrough project is created and added to Solution Explorer.
Creating the NorthwindDataSet
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.
-or-
Select New Connection to launch the Add/Modify Connection dialog box.
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.
Adding Controls to the Form
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
Drag the main Customers node from the Data Sources window onto Form1.
A DataGridView control and a tool strip (BindingNavigator) for navigating records appear on the form. A NorthwindDataSet, CustomersTableAdapter, BindingSource, and BindingNavigator appear in the component tray.
Inspecting the Generated Code That Fills the Dataset with Data
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.
Private Sub Form1_Load() Handles MyBase.Load 'TODO: This line of code loads data into the 'NorthwindDataSet1.Customers' table. 'You can move, or remove it, as needed. Me.CustomersTableAdapter1.Fill(Me.NorthwindDataSet1.Customers) End Sub
private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'northwindDataSet1.Customers' table. // You can move, or remove it, as needed. this.customersTableAdapter1.Fill(this.northwindDataSet1.Customers); }
You can optionally copy this code to other parts of your application where you need to fill the dataset.
Running the Application
To run the application
Press F5 to run the application.
The dataset is populated with data and is displayed in the DataGridView.
Next Steps
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.
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