Dela via


How to: Populate Documents with Data from Objects

Applies to

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

Document-level projects

  • Excel 2003

  • Excel 2007

  • Word 2003

  • Word 2007

Application-level projects

  • Excel 2007

  • Word 2007

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

Accesing data in a data object works the same way in document-level projects for Microsoft Office Word as it does in Windows Forms projects. You use the same tools and code to bring the data from an object into your solution, and you can use Windows Forms controls to display the data. In addition, you can display data by using host controls. Host controls are native objects in Microsoft Office Word that have been enhanced with events and data binding capability. For more information, see Host Items and Host Controls Overview.

You must complete three basic steps to populate the document with data from an object:

  • Add a control to the document that you can bind to data.

  • Add a data object to the document.

  • Connect the data object to the BindingSource.

Starting in Visual Studio 2008 Service Pack 1 (SP1), you can bind data to controls on documents or worksheets in application-level projects. If you do not have SP1 installed, you can bind data to controls only in document-level projects.

Adding a Data Object

To add a data object

Connecting the Data Object to the BindingSource

In document-level projects, you add controls to your document and bind them to data at design time.

In application-level projects, you create controls and bind them at run time.

Document-Level Projects

To connect the data object to the BindingSource

  1. Drag the data field you want from the Data Sources window to your document. This automatically creates a control.

  2. In your code, create an instance of the type of the object that you chose for the data source.

  3. Assign the instance to the DataSource property of the BindingSource.

Application-Level Projects

To connect the data object to the BindingSource

  1. In your code, create an instance of the type of the object that is associated with the data source.

  2. Create an instance of a BindingSource.

  3. Assign the data source instance to the DataSource property of the BindingSource.

  4. Add the data source as a databinding to the control.

See Also

Tasks

How to: Populate Documents with Data from a Database

How to: Update a Data Source with Data from a Host Control

Concepts

Binding Data to Controls in Office Solutions

Data Sources Overview

Displaying Data Overview

Connecting to Data in Visual Studio Overview

Reference

BindingSource Component Overview

Change History

Date

History

Reason

July 2008

Added information about data binding in application-level add-ins.

SP1 feature change.