Skip to content

Commit 7e1c6de

Browse files
committed
fix
1 parent 907deb0 commit 7e1c6de

File tree

1 file changed

+73
-89
lines changed

1 file changed

+73
-89
lines changed
Lines changed: 73 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Pass data between forms controls (.NET Framework)
33
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
4+
ms.date: 04/22/2025
55
ms.topic: how-to
66
dev_langs:
77
- VB
@@ -22,147 +22,133 @@ ms.subservice: data-tools
2222

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

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

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

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

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.
4539
4640
## Prerequisites
4741

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

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

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

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

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

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

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

6656
## Create the Windows Forms app project
6757

6858
1. In Visual Studio, on the **File** menu, select **New** > **Project**.
6959

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

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

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

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

7868
## Create the data source
7969

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

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

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

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

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

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

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

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

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

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

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

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

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

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

108-
### To create a data-bound grid on the form
98+
## Populate the first form
10999

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

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

114104
## Create the second form
115105

116106
Create a second form to pass data to.
117107

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)**.
121109

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

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

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

128-
The **OrdersBindingNavigator** disappears from **Form2**.
116+
1. Delete the **OrdersBindingNavigator** from the component tray. The **OrdersBindingNavigator** disappears from **Form2**.
129117

130118
## Add a TableAdapter query
131119

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

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

136-
2. Right-click the **OrdersTableAdapter**, and select **Add Query**.
124+
1. Right-click **OrdersTableAdapter** in the **Orders** table, and select **Add** > **Query**.
137125

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

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**.
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. 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:
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+
```sql
133+
SELECT OrderID, CustomerID, OrderDate, RequiredDate, ShippedDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry
134+
FROM Orders
135+
WHERE CustomerID = @CustomerID
136+
```
149137

150-
> [!NOTE]
151-
> Verify the correct parameter syntax for your database. For example, in Microsoft Access, the `WHERE` clause would look like: `WHERE CustomerID = ?`.
138+
> [!NOTE]
139+
> Use the correct parameter syntax for your database. For example, in Microsoft Access, the `WHERE` clause would look like `WHERE CustomerID = ?`.
152140
153-
6. Click **Next**.
141+
1. Select **Next**.
154142

155-
7. For the **Fill a DataTableMethod Name**, type `FillByCustomerID`.
143+
1. On the **Choose Methods to Generate** screen, select **Fill a DataTable** and enter *FillByCustomerID*.
156144

157-
8. Clear the **Return a DataTable** option, and then select **Next**.
158-
159-
9. Click **Finish**.
145+
1. Deselect the **Return a DataTable** option, and then select **Finish**.
160146

161147
## Create a method on Form2 to pass data to
162148

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

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

167153
### [C#](#tab/csharp)
168154

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

176162
## Create a method on Form1 to pass data and display Form2
177163

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

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

184-
The code editor appears.
168+
1. Double-click the **DoubleClick** event.
185169

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

188172
### [C#](#tab/csharp)
189173

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

197-
## Run the app
181+
## Run and test the app
198182

199-
- Press **F5** to run the application.
183+
1. Press **F5** to run the application.
200184

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

203187
## Next steps
204188

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

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

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

211195
## Related content
212196

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

Comments
 (0)