You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Pass data between forms controls (.NET Framework)
3
-
description: Pass data between Windows Forms controls in .NET Framework application development with Visual Studio and create a TableAdapter query to fetch customer orders.
4
-
ms.date: 12/15/2023
2
+
title: Pass data between forms (.NET Framework)
3
+
description: Pass data between Windows Forms in .NET Framework application development with Visual Studio and create a TableAdapter query that fetches orders per customer.
4
+
ms.date: 04/22/2025
5
5
ms.topic: how-to
6
6
dev_langs:
7
7
- VB
@@ -16,153 +16,141 @@ author: ghogen
16
16
ms.author: ghogen
17
17
manager: mijacobs
18
18
ms.subservice: data-tools
19
+
20
+
#customer intent: I want to know how to pass dataset data between forms in a Windows Forms (.NET Framework) application so that I understand one way to pass data between forms.
This tutorial provides step-by-step instructions for passing data from one form to another. Using the customers and orders tables from Northwind, one form allows users to select a customer, and a second form displays the selected customer's orders. This tutorial shows how to create a method on the second form that receives data from the first form.
26
-
27
-
> [!NOTE]
28
-
> This tutorial demonstrates only one way to pass data between forms. There are other options for passing data to a form, including creating a second constructor to receive data, or creating a public property that can be set with data from the first form.
29
-
30
-
Tasks illustrated in this tutorial include:
31
-
32
-
- Creating a new **Windows Forms App (.NET Framework)** project.
33
-
34
-
- Creating and configuring a dataset with the [Data Source Configuration Wizard](../data-tools/media/data-source-configuration-wizard.png).
35
-
36
-
- Selecting the control to be created on the form when dragging items from the **Data Sources** window. For more information, see [Set the control to be created when dragging from the Data Sources window](../data-tools/set-the-control-to-be-created-when-dragging-from-the-data-sources-window.md).
27
+
This tutorial provides step-by-step instructions for passing data from one form to another. By using the Customers and Orders tables from the Northwind sample database, one form allows users to select a customer, and a second form displays the selected customer's orders. This tutorial shows how to create a method on the second form that receives data from the first form.
37
28
38
-
- Creating a data-bound control by dragging items from the **Data Sources** window onto a form.
29
+
In this tutorial, you complete the following tasks:
39
30
40
-
- Creating a second form with a grid to display data.
31
+
- Create a new **Windows Forms App (.NET Framework)** project.
32
+
- Create and configure a dataset by using the **Data Source Configuration Wizard**.
33
+
- Select the control to create on the form when you drag items from the **Data Sources** window. For more information, see [Set the control to be created when dragging from the Data Sources window](set-the-control-to-be-created-when-dragging-from-the-data-sources-window.md).
34
+
- Create a data-bound control by dragging items from the **Data Sources** window onto a form.
35
+
- Create a second form with a grid that displays data.
36
+
- Create a `TableAdapter` query to fetch orders for a specific customer.
37
+
- Pass data between forms.
41
38
42
-
- Creating a TableAdapter query to fetch orders for a specific customer.
43
-
44
-
- Passing data between forms.
39
+
>[!NOTE]
40
+
>This tutorial demonstrates only one way to pass data between forms. There are other options for passing data to a form, such as creating a second constructor to receive data, or creating a public property that can be set with data from the first form.
45
41
46
42
## Prerequisites
47
43
48
-
To complete this tutorial, you need the **.NET desktop development** and **Data storage and processing** workloads installed in Visual Studio. To install them, open **Visual Studio Installer** and choose **Modify** (or **More** > **Modify**) next to the version of Visual Studio you want to modify. See [Modify Visual Studio](../install/modify-visual-studio.md).
49
-
50
-
This tutorial uses SQL Server Express LocalDB and the Northwind sample database.
51
-
52
-
1. If you don't have SQL Server Express LocalDB, install it either from the [SQL Server Express download page](https://www.microsoft.com/sql-server/sql-server-editions-express), or through the **Visual Studio Installer**. In the Visual Studio Installer, SQL Server Express LocalDB can be installed as part of the **Data storage and processing** workload, or as an individual component.
44
+
- The **.NET desktop development** and **Data storage and processing** workloads installed in Visual Studio. To install them, open **Visual Studio Installer** and choose **Modify**, or **More** > **Modify**, next to the version of Visual Studio you want to use. For more information, see [Modify Visual Studio](../install/modify-visual-studio.md).
53
45
54
-
2. Install the Northwind sample database by following these steps:
46
+
- SQL Server Express 2019 LocalDB installed either from the [SQL Server downloads page](https://www.microsoft.com/sql-server/sql-server-downloads), or through the **Visual Studio Installer** as part of the **Data storage and processing** workload.
55
47
56
-
1. In Visual Studio, open the **SQL Server Object Explorer** window. (SQL Server Object Explorer is installed as part of the **Data storage and processing** workload in the Visual Studio Installer.) Expand the **SQL Server** node. Right-click on your LocalDB instance and select **New Query**.
48
+
- The Northwind sample database, created by following these steps:
57
49
58
-
A query editor window opens.
50
+
1. In Visual Studio, open the **SQL Server Object Explorer**window by selecting it from the **View** menu. SQL Server Object Explorer is installed as part of the **Data storage and processing** workload.
59
51
60
-
2. Copy the [Northwind Transact-SQL script](https://github.com/MicrosoftDocs/visualstudio-docs/blob/main/docs/data-tools/samples/northwind.sql?raw=true) to your clipboard. This T-SQL script creates the Northwind database from scratch and populates it with data.
52
+
1. Expand the **SQL Server** node, right-click your LocalDB instance, and select **New Query** to open a query editor window.
61
53
62
-
3. Paste the T-SQL script into the query editor, and then choose the **Execute** button.
54
+
1. Copy the [Northwind Transact-SQL script](https://github.com/MicrosoftDocs/visualstudio-docs/blob/main/docs/data-tools/samples/northwind.sql?raw=true) to your clipboard. This T-SQL script creates the Northwind database and populates it with data.
63
55
64
-
After a short time, the query finishes running and the Northwind database is created.
56
+
1. Paste the T-SQL script into the query editor, and then select **Execute** at the top of the window to create the Northwind database.
65
57
66
58
## Create the Windows Forms app project
67
59
68
60
1. In Visual Studio, on the **File** menu, select **New** > **Project**.
69
61
70
-
2. Expand either **Visual C#** or **Visual Basic**in the left-hand pane, then select**WindowsDesktop**.
62
+
1. Select either **C#** or **Visual Basic**from the **All languages** dropdown list,**Windows** from the **All platforms** dropdown list, and **Desktop** from the **All project types** list.
71
63
72
-
3. In the middle pane, select the **Windows Forms App** project type.
64
+
1. Select **Windows Forms App (.NET Framework)**from the project templates list, and then select **Next**.
73
65
74
-
4. Name the project **PassingDataBetweenForms**, and then choose**OK**.
66
+
1. Name the project **PassingDataBetweenForms**, select **Next**, and then select**Create**.
75
67
76
-
The **PassingDataBetweenForms** project is created, and added to **Solution Explorer**.
68
+
The **PassingDataBetweenForms** project is created and added to **Solution Explorer**. The designer view of **Form1** appears in the editor.
77
69
78
70
## Create the data source
79
71
80
-
1.To open the **Data Sources** window, on the**Data**menu, click **Show Data Sources**.
72
+
1.Open the **Data Sources** window by selecting**Project**> **Add New Data Source** on the Visual Studio menu.
81
73
82
-
2. In the **Data Sources** window, select **Add New Data Source** to start the **Data Source Configuration** wizard.
74
+
1. In the **Data Sources** window, select **Add New Data Source** to start the **Data Source Configuration** wizard.
83
75
84
-
3. Select **Database** on the **Choose a Data Source Type** page, and then select **Next**.
76
+
1. On the **Choose a Data Source Type** page, select **Database**, and then select **Next**.
85
77
86
-
4. On the **Choose a database model** page, verify that **Dataset** is specified, and then select **Next**.
78
+
1. On the **Choose a Database Model** page, verify that **Dataset** is specified and select **Next**.
87
79
88
-
5. On the **Choose your Data Connection** page, do one of the following:
80
+
1. On the **Choose Your Data Connection** page, if a data connection to the Northwind sample database is already available, select it, and proceed to the **Choose your Database Objects** step. Otherwise, select **New Connection**.
89
81
90
-
- If a data connection to the Northwind sample database is available in the dropdown list, select it.
82
+
1. On the **Choose Data Source** screen, select **Microsoft SQL Server Database File**, and then select **Continue**.
91
83
92
-
- Select **New Connection**to launch the **Add/Modify Connection** dialog box.
84
+
1. On the **Add Connection** screen, select **Browse**, browse to and select the *northwnd.mdf* file, and select **Open**.
93
85
94
-
6. If your database requires a password and if the option to include sensitive data is enabled, select the option, and then select **Next**.
86
+
1. Either select **Windows Authentication**, or select **SQL Server Authentication** and supply a user name and password to access the database.
95
87
96
-
7. On the **Save connection string to the Application Configuration file** page, click**Next**.
88
+
1. Select **Test Connection**to test the connection. When the connection succeeds, select**OK**.
97
89
98
-
8. On the **Choose your Database Objects** page, expand the **Tables** node.
90
+
1. On the **Choose Your Data Connection** page, confirm that the database file appears and select **Next**.
99
91
100
-
9. Select the **Customers**and **Orders** tables, and then select **Finish**.
92
+
1. Select **No**to the popup question **Would you like to copy the file to your project and modify the connection?**
101
93
102
-
The **NorthwindDataSet** is added to your project, and the **Customers** and **Orders** tables appear in the **Data Sources** window.
94
+
1. On the **Save the Connection String to the Application Configuration file** page, select **Yes**. Name the connection *NorthwindConnectionString*, and then select **Next**.
103
95
104
-
## Create the first form (Form1)
96
+
1. On the **Choose your Database Objects** page, expand the **Tables** node, select the **Customers** and **Orders** tables, name the dataset *NorthwindDataSet*, and then select **Finish**.
105
97
106
-
You can create a data-bound grid (a <xref:System.Windows.Forms.DataGridView> control), by dragging the **Customers**node from the **Data Sources** window onto the form.
98
+
The **NorthwindDataSet** is added to your project, and the **Customers**and **Orders** tables appear in the **Data Sources** window.
107
99
108
-
### To create a data-bound grid on the form
100
+
##Populate the first form
109
101
110
-
-Drag the main**Customers** node from the **Data Sources** window onto **Form1**.
102
+
-To create a data-bound grid, drag the**Customers** node from the **Data Sources** window onto **Form1**.
111
103
112
-
A <xref:System.Windows.Forms.DataGridView> and a tool strip (<xref:System.Windows.Forms.BindingNavigator>) for navigating records appear on **Form1**. A [NorthwindDataSet](../data-tools/dataset-tools-in-visual-studio.md), CustomersTableAdapter, <xref:System.Windows.Forms.BindingSource>, and <xref:System.Windows.Forms.BindingNavigator> appear in the component tray.
104
+
A <xref:System.Windows.Forms.DataGridView>control and a <xref:System.Windows.Forms.BindingNavigator> tool strip for navigating records appear on **Form1**. A [NorthwindDataSet](dataset-tools-in-visual-studio.md), `CustomersTableAdapter`, <xref:System.Windows.Forms.BindingSource>, and <xref:System.Windows.Forms.BindingNavigator> appear in the component tray.
113
105
114
106
## Create the second form
115
107
116
108
Create a second form to pass data to.
117
109
118
-
1. From the **Project** menu, choose **Add Windows Form**.
110
+
1. From the **Project** menu, choose **Add Form (Windows Forms)**.
119
111
120
-
2. Leave the default name of **Form2**, and click**Add**.
112
+
1. Leave the default name of **Form2**, and select**Add**.
121
113
122
-
3. Drag the main **Orders** node from the **Data Sources** window onto **Form2**.
114
+
1. Drag the main **Orders** node from the **Data Sources** window onto **Form2**.
123
115
124
-
A <xref:System.Windows.Forms.DataGridView> and a tool strip (<xref:System.Windows.Forms.BindingNavigator>) for navigating records appear on **Form2**. A [NorthwindDataSet](../data-tools/dataset-tools-in-visual-studio.md), CustomersTableAdapter, <xref:System.Windows.Forms.BindingSource>, and <xref:System.Windows.Forms.BindingNavigator> appear in the component tray.
116
+
A <xref:System.Windows.Forms.DataGridView> and a <xref:System.Windows.Forms.BindingNavigator> tool strip for navigating records appear on **Form2**. A [NorthwindDataSet](dataset-tools-in-visual-studio.md), `CustomersTableAdapter`, <xref:System.Windows.Forms.BindingSource>, and <xref:System.Windows.Forms.BindingNavigator> appear in the component tray.
125
117
126
-
4. Delete the **OrdersBindingNavigator** from the component tray.
127
-
128
-
The **OrdersBindingNavigator** disappears from **Form2**.
118
+
1. Delete the **OrdersBindingNavigator** from the component tray. The **BindingNavigator** disappears from **Form2**.
129
119
130
120
## Add a TableAdapter query
131
121
132
-
Add a TableAdapter query to Form2 to load orders for the selected customer on Form1.
122
+
Add a `TableAdapter` query to Form2 that loads orders for the customer selected on Form1.
133
123
134
124
1. Double-click the **NorthwindDataSet.xsd** file in **Solution Explorer**.
135
125
136
-
2. Right-click the **OrdersTableAdapter**, and select **Add Query**.
137
-
138
-
3. Leave the default option of **Use SQL statements**, and then select **Next**.
126
+
1. Right-click **OrdersTableAdapter** in the **Orders** table, and select **Add** > **Query**.
139
127
140
-
4. Leave the default option of **SELECT which returns rows**, and then select **Next**.
128
+
1. Leave the default option of **Use SQL statements** and select **Next**.
141
129
142
-
5. Add a `WHERE` clause to the query, to return `Orders` based on the `CustomerID`. The query should be similar to the following:
130
+
1. Leave the default option of **SELECT which returns rows** and select **Next**.
1. In **What data should the table load**, to return `Orders` based on the `CustomerID`, add a `WHERE` clause to the end of the query. The final query should look similar to the following code:
149
133
150
-
> [!NOTE]
151
-
> Verify the correct parameter syntax for your database. For example, in Microsoft Access, the `WHERE` clause would look like: `WHERE CustomerID = ?`.
- Double-click a customer record in**Form1** to open **Form2** with that customer's orders.
187
+
1. In the application, double-click a customer record in **Form1** to open **Form2** with that customer's orders.
202
188
203
189
## Next steps
204
190
205
-
Depending on your application requirements, there are several steps you might want to perform after passing data between forms. Some enhancements you could make to this tutorial include:
191
+
Depending on your application requirements, you can do several steps after passing data between forms. Some enhancements you could make to this tutorial include:
206
192
207
-
- Editing the dataset to add or remove database objects. For more information, see [Create and configure datasets](../data-tools/create-and-configure-datasets-in-visual-studio.md).
193
+
- Editing the dataset to add or remove database objects. For more information, see [Create and configure datasets](create-and-configure-datasets-in-visual-studio.md).
208
194
209
-
- Adding functionality to save data back to the database. For more information, see [Save data back to the database](../data-tools/save-data-back-to-the-database.md).
195
+
- Adding functionality to save data back to the database. For more information, see [Save data back to the database](save-data-back-to-the-database.md).
210
196
211
197
## Related content
212
198
213
-
- [Bind Windows Forms controls to data in Visual Studio](../data-tools/bind-windows-forms-controls-to-data-in-visual-studio.md)
199
+
-[Bind Windows Forms controls to data in Visual Studio](bind-windows-forms-controls-to-data-in-visual-studio.md)
0 commit comments