Export (0) Print
Expand All

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:

The first step is to create a Windows Application.

To create the new Windows project

  1. In Visual Studio, from the File menu, create a new Project.

  2. Name the project DatasetWalkthrough.

  3. Select Windows Application and click OK. For more information, see Developing Client 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

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

  2. In the Data Sources window, click 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.

  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.

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

  1. In Solution Explorer, select Form1, and click the View Code button.

  2. Inspect the Form1_Load event handler. The TableAdapter's Fill method fills the dataset with data.

    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);
    }
    
  3. 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:

Show:
© 2014 Microsoft