Export (0) Print
Expand All
7 out of 31 rated this helpful - Rate this topic

Walkthrough: Passing Data Between Forms in a Windows Application 

This walkthrough provides step-by-step instructions for passing data from one form to another. Using the customers and orders tables from Northwind one form will allow users to select a customer and a second form will display the selected customer's orders. This walkthrough shows how to create a method on one form that receives data from the first form.

NoteNote

This walkthrough demonstrates only one way to pass data between forms. There are other options for passing data to a form, including these approaches: you can create a second constructor to receive data, or you can create a public property that can be set with data from the first form.

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 a form.

  • Creating a second form with a grid to display data.

  • Creating a TableAdapter query to fetch orders for a specific customer.

  • Passing data between forms.

In order to complete this walkthrough, you need:

To create the new Windows project

  1. From the File menu, create a new project.

  2. Name the project PassingDataBetweenForms.

  3. Select Windows Application and click OK. For more information, see Creating Windows-based Applications.

    The PassingDataBetweenForms project is created and added to Solution Explorer.

To create the Data Source

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

  2. In the Data Sources window, select 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. For more information, see Add/Modify Connection Dialog Box (General).

  5. If your database requires a password and if the option to include sensitive data is enabled, select the option 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 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.

You can create a data-bound grid (a DataGridView control) by dragging the Customers node from the Data Sources window onto the form.

To create a data-bound grid on the form

To create a second form to pass the data to

  1. From the Project menu, choose Add Windows Form.

  2. Leave the default name of Form2 and click Add.

  3. Drag the main Orders node from the Data Sources window onto Form2.

    A DataGridView and a tool strip (BindingNavigator) for navigating records appear on Form2. A NorthwindDataSet, CustomersTableAdapter, BindingSource, and BindingNavigator appear in the component tray.

  4. Delete the OrdersBindingNavigator from the component tray.

    The OrdersBindingNavigator disappears from Form2.

To create a TableAdapter Query

  1. Double-click the NorthwindDataSet.xsd file in Solution Explorer.

  2. Right-click the OrdersTableAdapter and select Add Query.

  3. Leave the default option of Use SQL statements, and then click Next.

  4. Leave the default option of SELECT which returns rows, and then click Next.

  5. Add a WHERE clause to the query to return Orders based on the CustomerID. The query should be similar to the following:

    SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate, ShippedDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry
    FROM Orders 
    WHERE CustomerID = @CustomerID
    
    NoteNote

    Verify the correct parameter syntax for your database. For example, in Microsoft Access, the WHERE clause would look like: WHERE CustomerID = ?.

  6. Click Next.

  7. For the Fill a DataTable Method Name, type FillByCustomerID.

  8. Clear the Return a DataTable option, and then click Next.

  9. Click Finish.

To create a method to pass data to

  1. Right-click Form2 and select View Code to open Form2 in the Code Editor.

  2. Replace the Form2_Load method that was automatically added to the form with the following code:

    internal void LoadOrders(String CustomerID)
    {
        ordersTableAdapter.FillByCustomerID(northwindDataSet.Orders, CustomerID);
    }
    
    
    protected void LoadOrders(String CustomerID)
    {
        northwindDataSetOrdersTableAdapter.FillByCustomerID(
            northwindDataSet.get_Orders(), CustomerID);
    }
    
    

To create a method to pass data to Form2

  1. Right-click Form1 and select View Code to open Form1 in the Code Editor.

  2. Add the following code to Form1 below the Form1_Load method:

    private void customersDataGridView_DoubleClick(object sender, EventArgs e)
    {
        System.Data.DataRowView SelectedRowView;
        NorthwindDataSet.CustomersRow SelectedRow;
    
        SelectedRowView = (System.Data.DataRowView)customersBindingSource.Current;
        SelectedRow = (NorthwindDataSet.CustomersRow)SelectedRowView.Row;
    
        Form2 OrdersForm = new Form2();
        OrdersForm.LoadOrders(SelectedRow.CustomerID);
        OrdersForm.Show();
    }
    
    
    private void customersDataGridView_DoubleClick(Object sender, System.EventArgs e)
    {
        System.Data.DataRowView SelectedRowView;
        NorthwindDataSet.CustomersRow SelectedRow;
    
        SelectedRowView = (System.Data.DataRowView)this.customersBindingSource.get_Current();
        SelectedRow = (NorthwindDataSet.CustomersRow)SelectedRowView.get_Row();
    
        Form2 OrdersForm = new Form2();
        OrdersForm.LoadOrders(SelectedRow.get_CustomerID());
        OrdersForm.Show();
    }
    
    

To run the application

  • Press F5 to run the application.

  • Double-click a customer record in Form1 to open Form2 with that customer's orders.

Depending on your application requirements, there are several steps you may want to perform after passing data between forms. Some enhancements you could make to this walkthrough include:

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.