Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Walkthrough: Binding Data to Controls on an Excel Actions Pane

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

Microsoft Office version

  • Excel 2007

  • Excel 2003

For more information, see Features Available by Application and Project Type.

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.

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.

NoteNote:

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 Visual Studio Settings.

You need the following components to complete this walkthrough:

  • Visual Studio Tools for Office (an optional component of Visual Studio 2008 Professional and Visual Studio Team System).

  • Microsoft Office Excel 2003 or Microsoft Office Excel 2007.

  • Access to a server with the Northwind SQL Server sample database.

  • Permissions to read from and write to the SQL Server database.

Visual Studio Tools for Office is installed by default with the listed versions of Visual Studio. To check whether it is installed, see Installing Visual Studio Tools for Office.

The first step is to create an Excel Workbook project.

To create a new 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 Visual Studio Tools for Office Projects.

    Visual Studio opens the new Excel workbook in the designer and adds the My Excel Actions Pane project to Solution Explorer.

To add a new data source to the project

  1. If the Data Sources window is not visible, click Show Data Sources on the Data menu.

    NoteNote:

    If Show Data Sources is not available, click inside the Excel worksheet and then check again.

  2. Click Add New Data Source to start the Data Source Configuration Wizard.

  3. Select Database and then click Next.

  4. Select a data connection to the Northwind sample SQL Server database, or add a new connection by using the New Connection button.

  5. Click Next.

  6. Clear the option to save the connection if it is selected, and then click Next.

  7. Expand the Tables node in the Database objects window.

  8. Select the check box next to the Suppliers table.

  9. Expand the Products table and select ProductName, SupplierID, QuantityPerUnit, and UnitPrice.

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

Next, add a NamedRange control and a ListObject control to the first worksheet.

To add a NamedRange control and a ListObject control

  1. Verify that the My Excel Actions Pane.xls workbook is open in the Visual Studio designer, with Sheet1 displayed.

  2. In the Data Sources window, expand the Suppliers table.

  3. Click the drop-down arrow on the Company Name node, and then click NamedRange.

  4. Drag Company Name from the Data Sources window to cell A2 in Sheet1.

    A NamedRange control named CompanyNameNamedRange is 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.

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

  6. Click ListObject in the drop-down list, and then drag the Products table to cell A6 in Sheet1.

    A ListObject control named ProductNameListObject is created in cell A6. At the same time, a BindingSource named productsBindingSource and a table adapter are added to the project. The control is bound to the BindingSource, which in turn is bound to the DataSet instance.

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

To add an actions pane control

  1. Select the My Excel Actions Pane project in Solution Explorer.

  2. On the Project menu, click Add New Item.

  3. In the Add New Item dialog box, select Actions Pane Control, name it ActionsControl, and click Add.

To add data-bound Windows Forms controls to an actions pane control

  1. From the Common Controls tabs of the Toolbox, drag a ComboBox control to the actions pane control.

  2. Change the Size property to 171, 21.

  3. Resize the user control to fit the combo box.

In this section, you will set the data source of the ComboBox to the same data source as the NamedRange control on the worksheet.

To set data binding properties of the control

  1. Right-click the actions pane control, and then click View Code.

  2. Add the following code to the Load event of the actions pane control.

    private void ActionsControl_Load(object sender, EventArgs e)
    {
        this.comboBox1.DataSource = Globals.Sheet1.suppliersBindingSource;
        this.comboBox1.DisplayMember = "CompanyName";
    }
    
  3. In C#, you must create an event handler for the ActionsControl. You can place this code in the ActionsControl constructor. For more information about creating event handlers, see How to: Create Event Handlers in Visual Studio Tools for Office.

    public ActionsControl()
    {
        InitializeComponent();
        this.Load += new EventHandler(ActionsControl_Load);
    }
    

The actions pane is not visible until you add the control at run time.

To show the actions pane

  1. In Solution Explorer, right-click ThisWorkbook.vb or ThisWorkbook.cs, and then click View Code.

  2. Create a new instance of the user control in the ThisWorkbook class.

    ActionsControl actions = new ActionsControl();
    
  3. In the Startup event handler of ThisWorkbook, add the control to the actions pane.

    private void ThisWorkbook_Startup(object sender, System.EventArgs e)
    {
        this.ActionsPane.Controls.Add(actions);
    }
    

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.

To test your document

  1. Press F5 to run your project.

  2. Confirm that the actions pane is visible.

  3. Select a company in the list box. Verify that the company name is listed in the NamedRange control and that the product details are listed in the ListObject control.

  4. Select various companies to verify the company name and product details change as appropriate.

Here are some tasks that might come next:

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft