Walkthrough: Binding Data to Controls on an Excel Actions Pane
This walkthrough demonstrates data binding to controls on an actions pane in Microsoft Office Excel. The controls demonstrate a master/detail relation between tables in a SQL Server database.
Applies to: The information in this topic applies to document-level projects for Excel. For more information, see Features Available by Office Application and Project Type.
This walkthrough illustrates the following tasks:
Adding controls to a worksheet.
Creating an actions pane control.
Adding data-bound Windows Forms controls to an actions pane control.
Showing the actions pane when the application opens.
You need the following components to complete this walkthrough:
An edition of Visual Studio that includes the Microsoft Office developer tools. For more information, see Configuring a Computer to Develop Office Solutions.
Excel 2013 or Excel 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 an Excel Workbook project.
Create an Excel Workbook project with the name My Excel 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 Excel workbook in the designer and adds the My Excel Actions Pane project to Solution Explorer.
If the Data Sources window is not visible, display it by, on the menu bar, choosing View, Other Windows, Data Sources.
Choose 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.
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 table.
Expand the Products table and select ProductName, SupplierID, QuantityPerUnit, and UnitPrice.
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.
Verify that the My Excel Actions Pane.xlsx workbook is open in the Visual Studio designer, with
In the Data Sources window, expand the Suppliers table.
Click the drop-down arrow on the Company Name node, and then click NamedRange.
Drag Company Name from the Data Sources window to cell A2 in
A NamedRange control named
CompanyNameNamedRangeis created, and the text <CompanyName> appears in cell A2. At the same time, a BindingSource named
suppliersBindingSource, a table adapter, and a DataSet are added to the project. The control is bound to the BindingSource, which in turn is bound to the DataSet instance.
In the Data Sources window, scroll down past the columns that are under the Suppliers table. At the bottom of the list is the Products table; it is here because it is a child of the Suppliers table. Select this Products table, not the one that is at the same level as the Suppliers table, and then click the drop-down arrow that appears.
Click ListObject in the drop-down list, and then drag the Products table to cell A6 in
A ListObject control named
ProductNameListObjectis created in cell A6. At the same time, a BindingSource named
productsBindingSourceand a table adapter are added to the project. The control is bound to the BindingSource, which in turn is bound to the DataSet instance.
For C# only, select suppliersBindingSource on the component tray, and change the Modifiers property to Internal in the Properties window.
Next, you need an actions pane control that contains a combo box.
Select the My Excel 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 click Add.
From the Common Controls tabs of the Toolbox, drag a ComboBox control to the actions pane control.
Change the Size property to 171, 21.
Resize the user control to fit the combo box.
Right-click the actions pane control, and then click View Code.
Add the following code to the Load event of the actions pane control.
In C#, you must create an event handler for the
ActionsControl. You can place this code in the
ActionsControlconstructor. For more information about creating event handlers, see How to: Create Event Handlers in Office Projects.
The actions pane is not visible until you add the control at run time.
In Solution Explorer, right-click ThisWorkbook.vb or ThisWorkbook.cs, and then click View Code.
Create a new instance of the user control in the
In the Startup event handler of
ThisWorkbook, add the control to the actions pane.
Now you can test your document to verify that the actions pane opens when the document is opened, and that the controls have a master/detail relation.
Press F5 to run your project.
Confirm that the actions pane is visible.
Select various companies to verify the company name and product details change as appropriate.
Here are some tasks that might come next:
Binding data to controls in Word. For more information, see Walkthrough: Binding Data to Controls on a Word Actions Pane.
Deploying the project. For more information, see Deploying an Office Solution by Using ClickOnce.