Walkthrough: Simple 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 Word document and bind the controls to data at run time.
Applies to: The information in this topic applies to VSTO add-in projects for Word. For more information, see Features Available by Office Application and Project Type.
This walkthrough illustrates the following tasks:
Adding a ContentControl to a document at run time.
Creating a BindingSource that connects the control to an instance of a dataset.
Enabling the user to scroll through the records and view them in the control.
|
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 IDE. |
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.
Word 2013 or Word 2010.
Access to a running instance of SQL Server 2005 or SQL Server 2005 Express that has the
AdventureWorksLTsample database attached to it. You can download theAdventureWorksLTdatabase from the CodePlex Web site. For more information about attaching a database, see the following topics:To attach a database by using SQL Server Management Studio or SQL Server Management Studio Express, see How to: Attach a Database (SQL Server Management Studio).
To attach a database by using the command line, see How to: Attach a Database File to SQL Server Express.
The first step is to create a Word VSTO Add-in project.
To create a new project
Create a Word VSTO Add-in project with the name Populating Documents 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 Documents from a Database project to Solution Explorer.
If your project targets the .NET Framework 4 or the .NET Framework 4.5, add a reference to the Microsoft.Office.Tools.Word.v4.0.Utilities.dll assembly. This reference is required to programmatically add Windows Forms controls to the document later in this walkthrough.
Use the Data Sources window to add a typed dataset to your project.
To add a typed dataset to the project
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.
Click Database, and then click Next.
If you have an existing connection to the
AdventureWorksLTdatabase, 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.
In the Save the Connection String to the Application Configuration File page, click Next.
In the Choose Your Database Objects page, expand Tables and select Customer (SalesLT).
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 Customer (SalesLT) table in the AdventureWorksLT database.A TableAdapter named
CustomerTableAdapter. This TableAdapter can be used to read and write data in theAdventureWorksLTDataSet. For more information, see TableAdapter Overview.
You will use both of these objects later in this walkthrough.
The interface for viewing database records in this walkthrough is very basic, and it is created right inside the document. One ContentControl displays a single database record at a time, and two Button controls enable you to scroll back and forth through the records. The content control uses a BindingSource to connect to the database.
For more information about binding controls to data, see Binding Data to Controls in Office Solutions.
To create the interface in the document
In the
ThisAddInclass, declare the following controls to display and scroll through theCustomertable of theAdventureWorksLTDataSetdatabase.In the
ThisAddIn_Startupmethod, add the following code to initialize the dataset, fill the dataset with information from theAdventureWorksLTDataSetdatabase.Add the following code to the
ThisAddIn_Startupmethod. This generates a host item that extends the document. For more information, see Extending Word Documents and Excel Workbooks in VSTO Add-ins at Run Time.Define several ranges at the beginning of the document. These ranges identify where to insert text and place controls.
Add the interface controls to the previously defined ranges.
Bind the content control to
AdventureWorksLTDataSetby using the BindingSource. For C# developers, add two event handlers for the Button controls.Add the following code to navigate through the database records.
When you open Word, the content control displays data from the AdventureWorksLTDataSet dataset. Scroll through the database records by clicking the Next and Previous buttons.
To test the VSTO Add-in
Press F5.
A content control named
customerContentControlis created and populated with data. At the same time, a dataset object namedadventureWorksLTDataSetand a BindingSource namedcustomerBindingSourceare added to the project. The ContentControl is bound to the BindingSource, which in turn is bound to the dataset object.Click the Next and Previous buttons to scroll through the database records.
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
Binding Windows Forms controls to data in Visual Studio
How to: Populate Documents with Data from Objects
How to: Update a Data Source with Data from a Host Control
Using Local Database Files in Office Solutions Overview
Connecting to Data in Windows Forms Applications
BindingSource Component Overview