How to: Populate worksheets with data from a database
You can access data in document-level Office projects in the same way that you access data in Windows Forms projects. You use the same tools and code to bring the data into your solution, and you can even use Windows Forms controls to display the data. In addition, you can take advantage of controls called host controls, which are native objects in Microsoft Office Excel 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 for Excel. For more information, see Features available by Office application and project type.
The following example shows how to add data-bound controls in document-level projects using a designer. For an example of how to add data-bound controls in application-level projects at run time, see Walkthrough: Complex data binding in VSTO Add-in project.
Add a data-bound control to a worksheet at design time
To populate a worksheet with data from a database
Open an Excel document-level project in Visual Studio, with the worksheet open in the designer.
Open the Data Sources window and create a data source for your project. For more information, see Add new connections.
Drag the field or table you want from the Data Sources window to your worksheet.
One of the following controls is created on the worksheet:
You can add a different control by selecting the table or field in the Data Sources window and then choosing a different control from the drop-down list.
Objects in the project
In addition to the control, the following data-related objects are automatically added to your project:
A typed dataset that encapsulates the data tables that you connected to in the database. For more information, see Dataset tools in Visual Studio.
A TableAdapter that connects the typed dataset to the database. For more information, see TableAdapter overview.
When you run the project, the control displays the first record in the data source. You can use the BindingSource to enable users to scroll through the records.
To scroll through the records
For information about how to send updates to the typed dataset and the database, see How to: Update a data source with data from a host control.
- Bind data to controls in Office solutions
- Add new data sources
- Bind Windows Forms controls to data in Visual Studio
- How to: Populate documents with data from objects
- How to: Populate documents with data from a database
- How to: Populate documents with data from services
- How to: Update a data source with data from a host control
- How do I: Transfer data into an Excel worksheet
- How do I: Consume database data in Excel?