The easiest way to bind data to a Bookmark control is to use the Data Sources window so that you can add to your document a control that is already bound to data. In this section you'll use the Data Sources window to add a connection to the Northwind database in Microsoft Access. You'll then bind some of the data to a Bookmark control.
1. Create a Word document solution using VSTO.
2. On the Data menu, click Add New Data Source.
3. In the Data Source Configuration Wizard, select Database, and then click Next, as shown in Figure 6.9.
4. Click New Connection to open the Add Connection dialog box.
5. Select Microsoft Access Database File for the data source, and select the Northwind.mdb database on your computer. This assumes that you have Microsoft Access installed on your computer.
6. For this example, you will not set a password to log on to the database. Click Test Connection, and then click OK to close the Add Connection dialog box (see Figure 6.10).
Enter information to connect to the selected data source or click "Change" to choose a different data source and/or provider. Data source:
¡Microsoft Access Database File (OLE DB) Change... | Database file name:
|C:\Program Files\Microsoft Office\OFFICEll' Browse... | "Log on to the database User name: j"
Save my password
Figure 6.10. Adding a connection to the Northwind database in Microsoft Access
7. On the Data Source Configuration Wizard, click Next.
8. When requested, copy the local data file to your project by clicking Yes, as shown in Figure 6.11. This action adds the data file to your project and gives you access to it in Solution Explorer.
9. Click Next on the Data Source Configuration Wizard using the default connection string.
10. Expand the Tables node, and click the check boxes for the Customers and Employees tables, as shown in Figure 6.12. Then click Finish.
A new dataset named NorthwindDataSet is added to the Data Sources window. The dataset contains Customers and Employees tables. Next you'll add a data-bound Bookmark control to the document.
1. Expand the Customers table, and click the CompanyName node. Click the arrow next to CompanyName to reveal a drop-down menu, as shown in Figure 6.13.
Next, you select the type of control that will be added when you drag the field to the document. Click the drop-down arrow next to the field, and then select the desired control. The default is the Bookmark control, indicated by the flag symbol. We'll use this control type when adding the field to the document.
2. Drag the CompanyName field from the Data Sources window to the document.
The bookmark is added to the document, and the bookmark's name appears between angle brackets in the document. This text will be replaced by the bound data when the solution is run (see Figures 6.14 and 6.15).
Because VSTO controls use ADO.NET for data binding, when you drag the Bookmark control from the Data Sources window, a Dataset, table adapter, and binding source are automatically created and added to the component tray (see Figure 6.14).
3. Press F5 to run the solution. The document displays the first record in the data source for the Company Name field, as shown in Figure 6.15.
|Company Name: Alfreds FutterAstt
Figure 6.15. Displaying the first record in a data-bound bookmark
You can add a button to the document and then add code to enable users to move through the records each time the button is clicked. You use the position of the binding source to move from record to record. VSTO automatically created a binding source named CustomersBindingSource when you dragged the bookmark to the document; you can use this binding source to move through the records. Listing 6.15 demonstrates how to write this code.
Listing 6.15. Moving through records in a dataset that are bound to a Bookmark control
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click
If Me.CustomersBindingSource.Position < _
Me.CustomersBindingSource.Count - 1 Then Me.CustomersBindingSource.MoveNext()
Me.CustomersBindingSource.MoveFirst() End If End Sub
If you want to bind data to an XMLNode control, you must do so pro-grammatically, because the XMLNode control is not available through the Data Sources window. If you map the schema described in Listing 6.5 to a Word document, you can add the code in Listing 6.16 to the Startup event handler of ThisDocument. This code binds a data source to the NameFirstNameNode and NameLastNameNode XMLNode controls that are created when you map the FirstName and LastName elements to the document.
Listing 6.16. Binding data to an XMLNode control
Private Sub ThisDocument_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup
Dim ds As DataSet = New DataSet()
Dim clientTable As DataTable = ds.Tables.Add("Clients")
.Columns.Add("FirstName", GetType(String)) .Columns.Add("LastName", GetType(String)) .Rows.Add("Mike", "Hernandez") .Rows.Add("Paul", "Stubbs") .Rows.Add("Kathleen", "McGrath") End With
Me.NameFirstNameNode.DataBindings.Add("Text", ds, _
"Clients.FirstName") Me.NameLastNameNode.DataBindings.Add("Text", ds, _ "Clients.LastName")
Was this article helpful?