Walkthrough: Complex Data Binding in VSTO add-in Project

Walkthrough: Complex Data Binding in VSTO add-in Project


You can bind data to host controls and Windows Forms controls in VSTO Add-in projects. This walkthrough demonstrates how to add controls to a Microsoft Office Excel worksheet and bind the controls to data at run time.

Applies to: The information in this topic applies to VSTO add-in projects for Excel. For more information, see Features Available by Office Application and Project Type.

This walkthrough illustrates the following tasks:

  • Adding a ListObject control to a worksheet at run time.

  • Creating a BindingSource that connects the control to an instance of a dataset.

System_CAPS_ICON_note.jpg 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 Personalizing the Visual Studio IDE.

You need the following components to complete this walkthrough:

The first step is to create an Excel VSTO Add-in project.

To create a new project

  1. Create an Excel VSTO Add-in project with the name Populating Worksheets from a Database, using either Visual Basic or C#.

    For more information, see How to: Create Office Projects in Visual Studio.

    Visual Studio opens the ThisAddIn.vb or ThisAddIn.cs file and adds the Populating Worksheets from a Database project to Solution Explorer.

Use the Data Sources window to add a typed dataset to your project.

To add a typed dataset to the project

  1. If the Data Sources window is not visible, display it by, on the menu bar, choosing View, Other Windows, Data Sources.

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

  3. Click Database, and then click Next.

  4. If you have an existing connection to the AdventureWorksLT database, choose this connection and click Next.

    Otherwise, click New Connection, and use the Add Connection dialog box to create the new connection. For more information, see How to: Connect to Data in a Database.

  5. In the Save the Connection String to the Application Configuration File page, click Next.

  6. In the Choose Your Database Objects page, expand Tables and select Address (SalesLT).

  7. Click Finish.

    The AdventureWorksLTDataSet.xsd file is added to Solution Explorer. This file defines the following items:

    • A typed dataset named AdventureWorksLTDataSet. This dataset represents the contents of the Address (SalesLT) table in the AdventureWorksLT database.

    • A TableAdapter named AddressTableAdapter. This TableAdapter can be used to read and write data in the AdventureWorksLTDataSet. For more information, see TableAdapter Overview.

    You will use both of these objects later in this walkthrough.

For this walkthrough, the ListObject control displays all the data in the table you selected as soon as the user opens the workbook. The list object uses a BindingSource to connect the control to the database.

For more information about binding controls to data, see Binding Data to Controls in Office Solutions.

To add the list object, dataset, and table adapter

  1. In the ThisAddIn class, declare the following controls to display the Address table of the AdventureWorksLTDataSet dataset.

        Private addressListObject As Microsoft.Office.Tools.Excel.ListObject
        Private adventureWorksDataSet As AdventureWorksLTDataSet
        Private addressTableAdapter As AdventureWorksLTDataSetTableAdapters.AddressTableAdapter
        Private addressBindingSource As System.Windows.Forms.BindingSource

  2. In the ThisAddIn_Startup method, add the following code to initialize the dataset and fill the dataset with information from the AdventureWorksLTDataSet dataset.

            Me.addressTableAdapter = New AdventureWorksLTDataSetTableAdapters.AddressTableAdapter()
            Me.adventureWorksDataSet = New AdventureWorksLTDataSet()
            Me.addressBindingSource = New System.Windows.Forms.BindingSource()

  3. Add the following code to the ThisAddIn_Startup method. This generates a host item that extends the worksheet. For more information, see Extending Word Documents and Excel Workbooks in VSTO Add-ins at Run Time.

            Dim worksheet As Excel.Worksheet = DirectCast(Me.Application.ActiveWorkbook.Worksheets(1), Excel.Worksheet)
            ' Create a workhseet host item.
            Dim extendedWorksheet As Worksheet = Globals.Factory.GetVstoObject(worksheet)

  4. Create a range and add the ListObject control.

            Dim cell As Excel.Range = extendedWorksheet.Range("$A$1:$G$5", System.Type.Missing)
            Me.addressListObject = extendedWorksheet.Controls.AddListObject(cell, "list1")

  5. Bind the list object to AdventureWorksLTDataSet by using the BindingSource. Pass in the names of the columns you want to bind to the list object.

            Me.addressBindingSource.DataSource = Me.adventureWorksDataSet.Address
            Me.addressListObject.AutoSetDataBoundColumnHeaders = True
            Me.addressListObject.SetDataBinding( _
                Me.addressBindingSource, "", "AddressID", "AddressLine1", _
                "AddressLine2", "City", "StateProvince", "CountryRegion", "PostalCode")

When you open Excel, the ListObject control displays the data from the Address table of the AdventureWorksLTDataSet dataset.

To test the VSTO Add-in

  • Press F5.

    A ListObject control named addressListObject is created in the worksheet. At the same time, a dataset object named adventureWorksLTDataSet and a BindingSource named addressBindingSource are added to the project. The ListObject is bound to the BindingSource, which in turn is bound to the dataset object.

Data in Office Solutions
Binding Data to Controls in Office Solutions
How to: Populate Worksheets with Data from a Database
How to: Populate Documents with Data from a Database
How to: Populate Documents with Data from Services
How to: Populate Documents with Data from Objects
How to: Scroll Through Database Records in a Worksheet
How to: Update a Data Source with Data from a Host Control
Walkthrough: Simple Data Binding in a Document-Level Project
Walkthrough: Complex Data Binding in a Document-Level Project
Using Local Database Files in Office Solutions Overview
Add new data sources
Entity with relative path '../Topic/Binding%20Windows%20Forms%20controls%20to%20data%20in%20Visual%20Studio.md' can not be found, for source topic '{"project_id":"3fedad16-eaf1-41a6-8f96-0c1949c68f32","entity_id":"ff52fb56-1f67-4ae2-a3d1-93038619d4e6","entity_type":"Article","locale":"en-US"}'.
Using Local Database Files in Office Solutions Overview
Connecting to Data in Windows Forms Applications
BindingSource Component Overview

© 2016 Microsoft