Skip to content

Commit 7638ebb

Browse files
Merge pull request #13855 from v-thepet/vs5
Visual Studio freshness #5
2 parents 4678107 + df3dda0 commit 7638ebb

File tree

1 file changed

+77
-91
lines changed

1 file changed

+77
-91
lines changed
Lines changed: 77 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
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
55
ms.topic: how-to
66
dev_langs:
77
- VB
@@ -16,153 +16,141 @@ author: ghogen
1616
ms.author: ghogen
1717
manager: mijacobs
1818
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.
1921
---
2022

2123
# Pass data between forms
2224

2325
[!INCLUDE [Data access tech note](./includes/data-technology-note.md)]
2426

25-
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.
3728

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:
3930

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.
4138

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.
4541
4642
## Prerequisites
4743

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).
5345

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.
5547

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:
5749

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.
5951

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.
6153

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.
6355

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.
6557

6658
## Create the Windows Forms app project
6759

6860
1. In Visual Studio, on the **File** menu, select **New** > **Project**.
6961

70-
2. Expand either **Visual C#** or **Visual Basic** in the left-hand pane, then select **Windows Desktop**.
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.
7163

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**.
7365

74-
4. Name the project **PassingDataBetweenForms**, and then choose **OK**.
66+
1. Name the project **PassingDataBetweenForms**, select **Next**, and then select **Create**.
7567

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.
7769

7870
## Create the data source
7971

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.
8173

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.
8375

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**.
8577

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**.
8779

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**.
8981

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**.
9183

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**.
9385

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.
9587

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**.
9789

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**.
9991

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?**
10193

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**.
10395

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**.
10597

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.
10799

108-
### To create a data-bound grid on the form
100+
## Populate the first form
109101

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**.
111103

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.
113105

114106
## Create the second form
115107

116108
Create a second form to pass data to.
117109

118-
1. From the **Project** menu, choose **Add Windows Form**.
110+
1. From the **Project** menu, choose **Add Form (Windows Forms)**.
119111

120-
2. Leave the default name of **Form2**, and click **Add**.
112+
1. Leave the default name of **Form2**, and select **Add**.
121113

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**.
123115

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.
125117

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**.
129119

130120
## Add a TableAdapter query
131121

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.
133123

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

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**.
139127

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**.
141129

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**.
143131

144-
```sql
145-
SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate, ShippedDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry
146-
FROM Orders
147-
WHERE CustomerID = @CustomerID
148-
```
132+
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:
149133

150-
> [!NOTE]
151-
> Verify the correct parameter syntax for your database. For example, in Microsoft Access, the `WHERE` clause would look like: `WHERE CustomerID = ?`.
134+
```sql
135+
SELECT OrderID, CustomerID, OrderDate, RequiredDate, ShippedDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry
136+
FROM Orders
137+
WHERE CustomerID = @CustomerID
138+
```
152139

153-
6. Click **Next**.
140+
> [!NOTE]
141+
> Use the correct parameter syntax for your database. For example, in Microsoft Access, the `WHERE` clause would look like `WHERE CustomerID = ?`.
154142
155-
7. For the **Fill a DataTableMethod Name**, type `FillByCustomerID`.
143+
1. Select **Next**.
156144

157-
8. Clear the **Return a DataTable** option, and then select **Next**.
145+
1. On the **Choose Methods to Generate** screen, select **Fill a DataTable** and enter *FillByCustomerID*.
158146

159-
9. Click **Finish**.
147+
1. Deselect the **Return a DataTable** option, and then select **Finish**.
160148

161149
## Create a method on Form2 to pass data to
162150

163-
1. Right-click **Form2**, and select **View Code** to open **Form2** in the **Code Editor**.
151+
1. Right-click in **Form2** and select **View Code** to open the **Form2** code in the editor.
164152

165-
2. Add the following code to **Form2** after the `Form2_Load` method:
153+
1. In the **Form2** code, remove the contents of the `Form2_Load` method, and add the following code after the `Form2_Load` method:
166154

167155
### [C#](#tab/csharp)
168156

@@ -175,15 +163,13 @@ Add a TableAdapter query to Form2 to load orders for the selected customer on Fo
175163

176164
## Create a method on Form1 to pass data and display Form2
177165

178-
1. In **Form1**, right-click the Customer data grid, and then select **Properties**.
179-
180-
2. In the **Properties** window, click **Events**.
166+
1. In the Designer view of **Form1**, right-click the Customer data grid, and then select **Properties**.
181167

182-
3. Double-click the **CellDoubleClick** event.
168+
1. In the **Properties** window, select the **Events** icon in the top toolbar.
183169

184-
The code editor appears.
170+
1. Double-click the **DoubleClick** event to open the code editor.
185171

186-
4. Update the method definition to match the following sample:
172+
1. In the code editor, update the method definition to match the following sample:
187173

188174
### [C#](#tab/csharp)
189175

@@ -194,20 +180,20 @@ Add a TableAdapter query to Form2 to load orders for the selected customer on Fo
194180
:::code language="vb" source="../snippets/visualbasic/VS_Snippets_VBCSharp/VbRaddataDisplaying/VB/Form1.vb" id="Snippet2":::
195181
---
196182

197-
## Run the app
183+
## Run and test the app
198184

199-
- Press **F5** to run the application.
185+
1. Press **F5** to run the application.
200186

201-
- 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.
202188

203189
## Next steps
204190

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:
206192

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).
208194

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).
210196

211197
## Related content
212198

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

Comments
 (0)