How to: Populate Documents with Data from Objects
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.
Applies to: The information in this topic applies to document-level projects and VSTO add-in projects for Excel and Word. For more information, see Features Available by Office Application and Project Type.
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.
- Open the Data Sources window and create a data source from an object. For more information, see How to: Connect to Data in Objects.
In document-level projects, you add controls to your document and bind them to data at design time.
In VSTO Add-in projects, you create controls and bind them at run time.
Drag the data field you want from the Data Sources window to your document. This automatically creates a control.
In your code, create an instance of the type of the object that you chose for the data source.
In your code, create an instance of the type of the object that is associated with the data source.
Create an instance of a BindingSource.
Add the data source as a databinding to the control.