Walkthrough: Passing Data Between Windows Forms
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.
Note
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.
Prerequisites
In order to complete this walkthrough, you need:
- Access to the Northwind sample database. For more information, see How to: Install Sample Databases.
Creating the Windows Application
To create the new Windows project
From the File menu, create a new project.
Name the project PassingDataBetweenForms.
Select Windows Forms Application and click OK. For more information, see Developing Client Applications with the .NET Framework.
The PassingDataBetweenForms project is created and added to Solution Explorer.
Creating the Data Source
To create the Data Source
On the Data menu, click Show Data Sources.
In the Data Sources window, select 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 a database model page, verify that Dataset is specified, 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 and if the option to include sensitive data is enabled, select the option 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 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 the First Form (Form1)
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
Drag the main Customers node from the Data Sources window onto Form1.
A DataGridView and a tool strip (BindingNavigator) for navigating records appear on Form1. A NorthwindDataSet, CustomersTableAdapter, BindingSource, and BindingNavigator appear in the component tray.
Creating the Second Form (Form2)
To create a second form to pass the data to
From the Project menu, choose Add Windows Form.
Leave the default name of Form2 and click Add.
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.
Delete the OrdersBindingNavigator from the component tray.
The OrdersBindingNavigator disappears from Form2.
Adding a TableAdapter Query to Form2 to Load Orders for the Selected Customer on Form1
To create a TableAdapter Query
Double-click the NorthwindDataSet.xsd file in Solution Explorer.
Right-click the OrdersTableAdapter and select Add Query.
Leave the default option of Use SQL statements, and then click Next.
Leave the default option of SELECT which returns rows, and then click Next.
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
Note
Verify the correct parameter syntax for your database. For example, in Microsoft Access, the WHERE clause would look like: WHERE CustomerID = ?.
Click Next.
For the Fill a DataTableMethod Name, type FillByCustomerID.
Clear the Return a DataTable option, and then click Next.
Click Finish.
Creating a Method on Form2 to Pass Data To
To create a method to pass data to
Right-click Form2 and select View Code to open Form2 in the Code Editor.
Add the following code to Form2 after the Form2_Load method:
Friend Sub LoadOrders(ByVal CustomerID As String) OrdersTableAdapter.FillByCustomerID(NorthwindDataSet.Orders, CustomerID) End Sub
internal void LoadOrders(String CustomerID) { ordersTableAdapter.FillByCustomerID(northwindDataSet.Orders, CustomerID); }
Creating a Method on Form1 to Pass Data and Display Form2
To create a method to pass data to Form2
In Form1, right-click the Customer data grid, and then click Properties.
In the Properties window, click Events.
Double-click the CellDoubleClick event.
The code editor appears.
Update the method definition to match the following sample:
Private Sub CustomersDataGridView_DoubleClick() Handles CustomersDataGridView.DoubleClick Dim SelectedRowView As Data.DataRowView Dim SelectedRow As NorthwindDataSet.CustomersRow SelectedRowView = CType(CustomersBindingSource.Current, System.Data.DataRowView) SelectedRow = CType(SelectedRowView.Row, NorthwindDataSet.CustomersRow) Dim OrdersForm As New Form2 OrdersForm.LoadOrders(SelectedRow.CustomerID) OrdersForm.Show() End Sub
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(); }
Run the Application
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.
Next Steps
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:
Editing the dataset, to add or remove database objects. For more information, see How to: Edit a Dataset.
Adding functionality to save data back to the database. For more information, see How to: Save Dataset Changes to a Database.
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