title | description | titleSuffix | ms.date | ms.topic | dev_langs | helpviewer_keywords | author | ms.author | manager | ms.subservice | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Walkthrough: Bind data to controls on a Word actions pane |
Bind data to controls on an actions pane in Microsoft Word. The controls demonstrate a master/detail relation between tables in a SQL Server database. |
02/02/2017 |
conceptual |
|
|
John-Hart |
johnhart |
mijacobs |
office-development |
This walkthrough demonstrates data binding to controls on an actions pane in Word. The controls demonstrate a master/detail relation between tables in a SQL Server database.
[!INCLUDEappliesto_wdalldoc]
This walkthrough illustrates the following tasks:
-
Creating an actions pane with Windows Forms controls that are bound to data.
-
Using a master/detail relationship to display data in the controls.
-
Show the actions pane when the application opens.
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalize the Visual Studio IDE.
You need the following components to complete this walkthrough:
-
[!INCLUDEvsto_vsprereq]
-
Word 2013 or Word 2010 .
-
Access to a server with the Northwind SQL Server sample database.
-
Permissions to read from and write to the SQL Server database.
The first step is to create a Word Document project.
-
Create a Word Document project with the name My Word Actions Pane. In the wizard, select Create a new document.
For more information, see How to: Create Office projects in Visual Studio.
Visual Studio opens the new Word document in the designer and adds the My Word Actions Pane project to Solution Explorer.
For this walkthrough, you need an actions pane control that contains data-bound Windows Forms controls. Add a data source to the project, and then drag controls from the Data Sources window to the actions pane control.
-
Select the My Word Actions Pane project in Solution Explorer.
-
On the Project menu, click Add New Item.
-
In the Add New Item dialog box, select Actions Pane Control, name it ActionsControl, and then click Add.
-
If the Data Sources window is not visible, display it by, on the menu bar, choosing View > Other Windows > Data Sources.
[!NOTE] If Show Data Sources is not available, click the Word document and then check again.
-
Click Add New Data Source to start the Data Source Configuration Wizard.
-
Select Database and then click Next.
-
Select a data connection to the Northwind sample SQL Server database, or add a new connection by using the New Connection button.
-
Click Next.
-
Clear the option to save the connection if it is selected, and then click Next.
-
Expand the Tables node in the Database objects window.
-
Select the check box next to the Suppliers and Products tables.
-
Click Finish.
The wizard adds the Suppliers table and Products table to the Data Sources window. It also adds a typed dataset to your project that is visible in Solution Explorer.
-
In the Data Sources window, expand the Suppliers table.
-
Click the drop-down arrow on the Company Name node, and select ComboBox.
-
Drag CompanyName from the Data Sources window to the actions pane control.
A xref:System.Windows.Forms.ComboBox control is created on the actions pane control. At the same time, a xref:System.Windows.Forms.BindingSource named
SuppliersBindingSource
, a table adapter, and a xref:System.Data.DataSet are added to the project in the component tray. -
Select
SuppliersBindingNavigator
in the Component tray and press Delete. You will not use theSuppliersBindingNavigator
in this walkthrough.[!NOTE] Deleting the
SuppliersBindingNavigator
does not remove all of the code that was generated for it. You can remove this code. -
Move the combo box so that it is under the label and change the Size property to 171, 21.
-
In the Data Sources window, expand the Products table that is a child of the Suppliers table.
-
Click the drop-down arrow on the ProductName node, and select ListBox.
-
Drag ProductName to the actions pane control.
A xref:System.Windows.Forms.ListBox control is created on the actions pane control. At the same time, a xref:System.Windows.Forms.BindingSource named
ProductBindingSource
and a table adapter are added to the project in the component tray. -
Move the list box so that it is under the label and change the Size property to 171,95.
-
Drag a xref:System.Windows.Forms.Button from the Toolbox onto the actions pane control and place it below the list box.
-
Right-click the xref:System.Windows.Forms.Button, click Properties on the shortcut menu, and change the following properties.
Property Value Name Insert Text Insert -
Resize the user control to fit the controls.
To set up the data source, add code to the xref:System.Windows.Forms.UserControl.Load event of the actions pane control to fill the control with data from the xref:System.Data.DataTable, and set the xref:System.Windows.Forms.Binding.DataSource%2A and xref:System.Windows.Forms.BindingSource.DataMember%2A properties for each control.
-
In the xref:System.Windows.Forms.UserControl.Load event handler of the
ActionsControl
class, add the following code.:::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreActionsPaneWordCS/ActionsControl.cs" id="Snippet1":::
-
In C#, you must attach the event handler to the xref:System.Windows.Forms.UserControl.Load event. You can place this code in the
ActionsControl
constructor, after the call toInitializeComponent
. For more information about how to create event handlers, see How to: Create event handlers in Office projects.:::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreActionsPaneWordCS/ActionsControl.cs" id="Snippet33":::
-
Select the
CompanyNameComboBox
control. -
In the Properties window, click the button to the right of the DataSource property, and select suppliersBindingSource.
-
Click the button to the right of the DisplayMember property, and select CompanyName.
-
Expand the DataBindings property, click the button to the right of the Text property, and select None.
-
Select the
ProductNameListBox
control. -
In the Properties window, click the button to the right of the DataSource property, and select productsBindingSource.
-
Click the button to the right of the DisplayMember property, and select ProductName.
-
Expand the DataBindings property, click the button to the right of the SelectedValue property, and select None.
The next task is to read the data from the bound controls and populate a table in your Word document. First, create a procedure for formatting the headings in the table, and then add the AddData
method to create and format a Word table.
-
In the
ActionsControl
class, create a method to format the headings of the table.:::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreActionsPaneWordCS/ActionsControl.cs" id="Snippet2":::
-
In the
ActionsControl
class, write a method that will create a table if one does not already exist, and add data from the actions pane to the table.:::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreActionsPaneWordCS/ActionsControl.cs" id="Snippet3":::
-
Add the following code to the xref:System.Windows.Forms.Control.Click event handler of the Insert button.
:::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreActionsPaneWordCS/ActionsControl.cs" id="Snippet4":::
-
In C#, you must create an event handler for the xref:System.Windows.Forms.Control.Click event of the button. You can place this code in the xref:System.Windows.Forms.UserControl.Load event handler of the
ActionsControl
class.:::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreActionsPaneWordCS/ActionsControl.cs" id="Snippet5":::
The actions pane becomes visible after controls are added to it.
-
In Solution Explorer, right-click ThisDocument.vb or ThisDocument.cs, and then click View Code on the shortcut menu.
-
Create a new instance of the control at the top of the
ThisDocument
class so that it looks like the following example.:::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreActionsPaneWordCS/ThisDocument.cs" id="Snippet6":::
-
Add code to the xref:Microsoft.Office.Tools.Word.Document.Startup event handler of
ThisDocument
so that it looks like the following example.:::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreActionsPaneWordCS/ThisDocument.cs" id="Snippet7":::
Now you can test your document to verify that the actions pane appears when the document is opened. Test for the master/detail relationship in the controls on the actions pane, and make sure that data is populated in a Word table when the Insert button is clicked.
-
Press F5 to run your project.
-
Confirm that the actions pane is visible.
-
Select a company in the combo box and verify that the items in the Products list box change.
-
Select a product, click Insert on the actions pane, and verify that the product details are added to the table in Word.
-
Insert additional products from various companies.
This walkthrough shows the basics of binding data to controls on an actions pane in Word. Here are some tasks that might come next:
-
Binding data to controls in Excel. For more information, see Walkthrough: Bind data to controls on an Excel actions pane.
-
Deploying the project. For more information, see Deploy an Office solution by using ClickOnce.