Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Walkthrough: Complex Data Binding in an Application-Level Project

Updated: July 2008

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

  • Application-level projects

Microsoft Office version

  • Excel 2007

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

Starting in Visual Studio 2008 Service Pack 1 (SP1), you can bind data to host controls and Windows Forms controls in application-level projects. This walkthrough demonstrates how to add controls to a Microsoft Office Excel worksheet and bind the controls to data at run time.

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.

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

    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.

  • Excel 2007.

  • Access to a running instance of SQL Server 2005 or SQL Server 2005 Express that has the AdventureWorksLT sample database attached to it. You can download the AdventureWorksLT database from the CodePlex Web site. For more information about attaching a database, see the following topics:

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

To create a new project

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

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

    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. On the Data menu, click Add New Data Source.

    The Data Source Configuration Wizard opens.

  2. Click Database, and then click Next.

  3. 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: Create Connections to SQL Server Databases.

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

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

  6. 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 Microsoft.Office.Tools.Excel.ListObject addressListObject;
    private AdventureWorksLTDataSet adventureWorksDataSet;
    private AdventureWorksLTDataSetTableAdapters.AddressTableAdapter addressTableAdapter;
    private System.Windows.Forms.BindingSource addressBindingSource;
    
  2. In the ThisAddIn_Startup method, add the following code to initialize the dataset and fill the dataset with information from the AdventureWorksLTDataSet dataset.

    this.addressTableAdapter = new AdventureWorksLTDataSetTableAdapters.AddressTableAdapter();
    this.adventureWorksDataSet = new AdventureWorksLTDataSet();
    this.addressTableAdapter.Fill(this.adventureWorksDataSet.Address);
    this.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 Application-Level Add-ins at Run Time.

    Excel.Worksheet worksheet = (Excel.Worksheet)this.Application.ActiveWorkbook.Worksheets[1];
    Worksheet extendedWorksheet = worksheet.GetVstoObject();
    
  4. Create a range and add the ListObject control.

    Excel.Range cell = extendedWorksheet.Range["$A$1:$G$5", System.Type.Missing];
    this.addressListObject = extendedWorksheet.Controls.AddListObject(cell, "list1");
    
  5. Bind the content control to AdventureWorksLTDataSet by using the BindingSource. Pass in the names of the columns you want to bind to the list object.

    this.addressBindingSource.DataSource = this.adventureWorksDataSet.Address;
    this.addressListObject.AutoSetDataBoundColumnHeaders = true;
    this.addressListObject.SetDataBinding(
        this.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 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.

Date

History

Reason

July 2008

Added topic.

SP1 feature change.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.