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
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.
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.
25
+
This tutorial provides step-by-step instructions for passing data from one form to another. 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.
26
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.
27
+
In this tutorial, you complete the following tasks:
29
28
30
-
Tasks illustrated in this tutorial include:
29
+
- Create a new **Windows Forms App (.NET Framework)** project.
30
+
- Create and configure a dataset by using the **Data Source Configuration Wizard**.
31
+
- 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).
32
+
- Create a data-bound control by dragging items from the **Data Sources** window onto a form.
33
+
- Create a second form with a grid to display data.
34
+
- Create a `TableAdapter` query to fetch orders for a specific customer.
35
+
- Pass data between forms.
31
36
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).
37
-
38
-
- Creating a data-bound control by dragging items from the **Data Sources** window onto a form.
39
-
40
-
- Creating a second form with a grid to display data.
41
-
42
-
- Creating a TableAdapter query to fetch orders for a specific customer.
43
-
44
-
- Passing data between forms.
37
+
>[!NOTE]
38
+
>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
39
46
40
## Prerequisites
47
41
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).
42
+
- 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).
49
43
50
-
This tutorial uses SQL Server Express LocalDB and the Northwind sample database.
44
+
-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.
51
45
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.
46
+
- The Northwind sample database, created by following these steps:
53
47
54
-
2. Install the Northwind sample database by following these steps:
48
+
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.
55
49
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**.
50
+
1. Expand the **SQL Server** node, right-click your LocalDB instance, and select **New Query** to open a query editor window.
57
51
58
-
A query editor window opens.
52
+
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.
59
53
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.
61
-
62
-
3. Paste the T-SQL script into the query editor, and then choose the **Execute** button.
63
-
64
-
After a short time, the query finishes running and the Northwind database is created.
54
+
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
55
66
56
## Create the Windows Forms app project
67
57
68
58
1. In Visual Studio, on the **File** menu, select **New** > **Project**.
69
59
70
-
2. Expand either **Visual C#** or **Visual Basic**in the left-hand pane, then select**WindowsDesktop**.
60
+
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
61
72
-
3. In the middle pane, select the **Windows Forms App** project type.
62
+
1. Select **Windows Forms App (.NET Framework)**, and then select **Next**.
73
63
74
-
4. Name the project **PassingDataBetweenForms**, and then choose**OK**.
64
+
1. Name the project **PassingDataBetweenForms**, select **Next**, and then select**Create**.
75
65
76
-
The **PassingDataBetweenForms** project is created, and added to **Solution Explorer**.
66
+
The **PassingDataBetweenForms** project is created and added to **Solution Explorer**. The designer view of **Form1** appears in the editor.
77
67
78
68
## Create the data source
79
69
80
-
1.To open the **Data Sources** window, on the**Data**menu, click **Show Data Sources**.
70
+
1.Open the **Data Sources** window by selecting**Project**> **Add New Data Source** on the Visual Studio menu.
81
71
82
-
2. In the **Data Sources** window, select **Add New Data Source** to start the **Data Source Configuration** wizard.
72
+
1. In the **Data Sources** window, select **Add New Data Source** to start the **Data Source Configuration** wizard.
83
73
84
-
3. Select **Database** on the **Choose a Data Source Type** page, and then select **Next**.
74
+
1. On the **Choose a Data Source Type** page, select **Database**, and then select **Next**.
85
75
86
-
4. On the **Choose a database model** page, verify that **Dataset** is specified, and then select **Next**.
76
+
1. On the **Choose a Database Model** page, verify that **Dataset** is specified and select **Next**.
87
77
88
-
5. On the **Choose your Data Connection** page, do one of the following:
78
+
1. On the **Choose Your Data Connection** page, if a data connection to the Northwind sample database is available, select it, and proceed to the **Choose your Database Objects** step. Otherwise, select **New Connection**.
89
79
90
-
- If a data connection to the Northwind sample database is available in the dropdown list, select it.
80
+
1. On the **Choose Data Source** screen, select **Microsoft SQL Server Database File**, and then select **Continue**.
91
81
92
-
- Select **New Connection**to launch the **Add/Modify Connection** dialog box.
82
+
1. On the **Add Connection** screen, select **Browse**, browse to and select the *northwnd.mdf* file, and select **Open**.
93
83
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**.
84
+
1. Either select **Windows Authentication**, or select **SQL Server Authentication** and supply a user name and password to access the database.
95
85
96
-
7. On the **Save connection string to the Application Configuration file** page, click**Next**.
86
+
1. Select **Test Connection**to test the connection. When the connection succeeds, select**OK**.
97
87
98
-
8. On the **Choose your Database Objects** page, expand the **Tables** node.
88
+
1. On the **Choose Your Data Connection** page, confirm that the database file appears and select **Next**.
99
89
100
-
9. Select the **Customers**and **Orders** tables, and then select **Finish**.
90
+
1. Select **No**to the popup question **Would you like to copy the file to your project and modify the connection?**
101
91
102
-
The **NorthwindDataSet** is added to your project, and the **Customers** and **Orders** tables appear in the **Data Sources** window.
92
+
1. On the **Save the Connection String to the Application Configuration file** page, select **Yes**. Name the connection *NorthwindConnectionString*, and then select **Next**.
103
93
104
-
## Create the first form (Form1)
94
+
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
95
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.
96
+
The **NorthwindDataSet** is added to your project, and the **Customers**and **Orders** tables appear in the **Data Sources** window.
107
97
108
-
### To create a data-bound grid on the form
98
+
##Populate the first form
109
99
110
-
- Drag the main**Customers** node from the **Data Sources** window onto **Form1**.
100
+
To create a data-bound grid, drag the**Customers** node from the **Data Sources** window onto **Form1**.
111
101
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.
102
+
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
103
114
104
## Create the second form
115
105
116
106
Create a second form to pass data to.
117
107
118
-
1. From the **Project** menu, choose **Add Windows Form**.
119
-
120
-
2. Leave the default name of **Form2**, and click **Add**.
108
+
1. From the **Project** menu, choose **Add Form (Windows Forms)**.
121
109
122
-
3. Drag the main **Orders** node from the **Data Sources** window onto**Form2**.
110
+
1. Leave the default name of **Form2**, and select**Add**.
123
111
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.
112
+
1. Drag the main **Orders** node from the **Data Sources** window onto **Form2**.
125
113
126
-
4. Delete the **OrdersBindingNavigator** from the component tray.
114
+
A <xref:System.Windows.Forms.DataGridView> and a tool strip (<xref:System.Windows.Forms.BindingNavigator>) 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.
127
115
128
-
The **OrdersBindingNavigator** disappears from **Form2**.
116
+
1. Delete the **OrdersBindingNavigator** from the component tray. The **OrdersBindingNavigator** disappears from **Form2**.
129
117
130
118
## Add a TableAdapter query
131
119
132
-
Add a TableAdapter query to Form2 to load orders for the selected customer on Form1.
120
+
Add a `TableAdapter` query to Form2 that loads orders for the customer selected on Form1.
133
121
134
122
1. Double-click the **NorthwindDataSet.xsd** file in **Solution Explorer**.
135
123
136
-
2. Right-click the **OrdersTableAdapter**, and select **AddQuery**.
124
+
1. Right-click **OrdersTableAdapter** in the **Orders** table, and select **Add** > **Query**.
137
125
138
-
3. Leave the default option of **Use SQL statements**, and then select **Next**.
126
+
1. Leave the default option of **Use SQL statements** and select **Next**.
139
127
140
-
4. Leave the default option of **SELECT which returns rows**, and then select **Next**.
128
+
1. Leave the default option of **SELECT which returns rows** 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. 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 query should look similar to the following:
- Double-click a customer record in**Form1** to open **Form2** with that customer's orders.
185
+
1. In the application, double-click a customer record in **Form1** to open **Form2** with that customer's orders.
202
186
203
187
## Next steps
204
188
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:
189
+
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
190
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).
191
+
- 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
192
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).
193
+
- 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
194
211
195
## Related content
212
196
213
-
- [Bind Windows Forms controls to data in Visual Studio](../data-tools/bind-windows-forms-controls-to-data-in-visual-studio.md)
197
+
-[Bind Windows Forms controls to data in Visual Studio](bind-windows-forms-controls-to-data-in-visual-studio.md)
0 commit comments