Walkthrough: Creating a Master/Detail Relation Using a Cached Dataset
Applies to |
---|
The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office. Project type
Microsoft Office version
For more information, see Features Available by Application and Project Type. |
This walkthrough demonstrates creating a master/detail relation on a worksheet, and caching the data so that the solution can be used offline.
During this walkthrough, you will learn how to:
Add controls to a worksheet.
Set up a dataset to be cached in a worksheet.
Add code to enable scrolling through the records.
Test your project.
Note
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 Visual Studio Settings.
Prerequisites
To complete this walkthrough, you will need:
Visual Studio Tools for Office (an optional component of Visual Studio 2008 Professional and Visual Studio Team System).
Microsoft Office Excel 2003 or Microsoft Office Excel 2007.
Access to the Northwind SQL Server sample database. The database can be on your development computer or on a server.
Permissions to read from and write to the SQL Server database.
Creating a New Project
In this step, you will create an Excel Workbook project.
To create a new project
- Create an Excel Workbook project with the name My Master-Detail, using either Visual Basic or C#. Make sure that Create a new document is selected. For more information, see How to: Create Visual Studio Tools for Office Projects.
Visual Studio opens the new Excel workbook in the designer and adds the My Master-Detail project to Solution Explorer.
Creating the Data Source
Use the Data Sources window to add a typed dataset to your project.
To create the data source
If the Data Sources window is not visible, click Show Data Sources on the Data menu.
Click Add New Data Source to start the Data Source Configuration Wizard.
Select Database and then click Next.
Select a data connection to the Northwind sample SQL Server database, or add a new connection by using the New Connection button.
After selecting or creating a connection, click Next.
Clear the option to save the connection if it is selected, and then click Next.
Expand the Tables node in the Database objects window.
Select the Orders table and the Order Details table.
Click Finish.
The wizard adds the two tables to the Data Sources window. It also adds a typed dataset to your project that is visible in Solution Explorer.
Adding Controls to the Worksheet
In this step, you will add a named range, a list object, and two buttons to the first worksheet. First, add the named range and the list object from the Data Sources window so that they are automatically bound to the data source. Next, add the buttons from the Toolbox.
To add a named range and a list object
Verify that the My Master-Detail.xls workbook is open in the Visual Studio designer, with Sheet1 displayed.
Open the Data Sources window and expand the Orders node.
Select the OrderID column, and then click the drop-down arrow that appears.
Click NamedRange in the drop-down list, and then drag the OrderID column to cell A2.
A NamedRange control named OrderIDNamedRange is created in cell A2. At the same time, a BindingSource named OrdersBindingSource, a table adapter, and a DataSet instance are added to the project. The control is bound to the BindingSource, which in turn is bound to the DataSet instance.
Scroll down past the columns that are under the Orders table. At the bottom of the list is the Order Details table; it is here because it is a child of the Orders table. Select this Order Details table, not the one that is at the same level as the Orders table, and then click the drop-down arrow that appears.
Click ListObject in the drop-down list, and then drag the OrderDetails table to cell A6.
A ListObject control named Order_DetailsListObject is created in cell A6, and bound to the BindingSource.
To add two buttons
From the Common Controls tab of the Toolbox, add a Button control to cell A3 of the worksheet.
This button is named Button1.
Add another Button control to cell B3 of the worksheet.
This button is named Button2.
Next, mark the dataset to be cached in the document.
Caching the Dataset
Mark the dataset to be cached in the document by making the dataset public and setting the CacheInDocument property.
To cache the dataset
Select NorthwindDataSet in the component tray.
In the Properties window, change the Modifiers property to Public.
Datasets must be public before caching is enabled.
Change the CacheInDocument property to True.
The next step is to add text to the buttons, and in C# add code to hook up the event handlers.
Initializing the Controls
Set the button text and add event handlers during the Startup event.
To initialize the data and the controls
In Solution Explorer, right-click Sheet1.vb or Sheet1.cs, and then click View Code on the shortcut menu.
Add the following code to the Sheet1_Startup method to set the text for the buttons.
Me.Button1.Text = "<" Me.Button2.Text = ">"
this.button1.Text = "<"; this.button2.Text = ">";
For C# only, add event handlers for the button click events to the Sheet1_Startup method.
this.button1.Click += new EventHandler(button1_Click); this.button2.Click += new EventHandler(button2_Click);
Adding Code to Enable Scrolling Through the Records
Add code to the Click event handler of each button to move through the records.
To scroll through the records
Add an event handler for the Click event of Button1, and add the following code to move backwards through the records:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button1.Click Me.OrdersBindingSource.MovePrevious() End Sub
private void button1_Click(object sender, EventArgs e) { this.ordersBindingSource.MovePrevious(); }
Add an event handler for the Click event of Button2, and add the following code to advance through the records:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button2.Click Me.OrdersBindingSource.MoveNext() End Sub
private void button2_Click(object sender, EventArgs e) { this.ordersBindingSource.MoveNext(); }
Testing the Application
Now you can test your workbook to make sure that the data appears as expected, and that you can use the solution offline.
To test the data caching
Press F5.
Verify that the named range and the list object are filled with data from the data source.
Scroll through some of the records by clicking the buttons.
Save the workbook, and then close the workbook and Visual Studio.
Disable the connection to the database. Unplug the network cable from your computer if the database is located on a server, or stop the SQL Server service if the database is on your development computer.
Open Excel, and then open My Master-Detail.xls from the \bin directory (\My Master-Detail\bin in Visual Basic or \My Master-Detail\bin\debug in C#).
Scroll through some of the records to see that the worksheet operates normally when disconnected.
Reconnect to the database. Connect your computer to the network again if the database is located on a server, or start the SQL Server service if the database is on your development computer.
Next Steps
This walkthrough shows the basics of creating a master/detail data relationship on a worksheet and caching a dataset. Here are some tasks that might come next:
Deploy the solution. For more information, see Deploying Office Solutions
Retrieve data from the cache for another project. For more information, see How to: Retrieve Cached Data from a Workbook on a Server.
See Also
Concepts
Binding Data to Controls in Office Solutions
Host Items and Host Controls Overview