Working with Related Data

VSTO makes it easy to display related data in a document so that when you scroll through data records in one table, data in related tables is automatically updated. For example, you might want to view the details of product orders placed by a particular company. You can do this by dragging controls from the Data Sources window to your worksheet. In this case, let's use Sheet2.

1. In the Data Sources Window, expand the Suppliers table, and drag a CompanyName NamedRange from the Data Sources window to cell A2 of Sheet2.

Notice that at the end of the Customers table, there is a Products table; this indicates a relationship between the Products table and the Suppliers table.

2. Drag the entire Products table to cell A5 of Sheet2.

3. Drag a Button control from the Toolbox to cell E2, and change its Text property to Next.

4. Add the code in Listing 7.23 to the Click event handler of Button1. Listing 7.23. Moving through records in a dataset

Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click

Me.SuppliersBindingSource.MoveNext()

Press F5 to run the code. Notice that the product changes along with the supplier's name whenever you click Button1 (see Figure 7.26).

File Edit View Insert Format Ipols Data Window Help Type a question for help ▼ _ ff

[ Arial Li 10 z B I U S= » = HÜ | $ % » töo i'o I ÎF =F 1 ' ^ " A ' ^

! J J A ui j 1 & A1 V & 1 A ^ ¿a - J \ -----1À s - ZA 1 m -ö - # 1

CompanyNa... ▼ fx New Orleans Cajun Delights

A

B

C

D

E

F

G

H

I

J

1

2

New Orleani

Cajun Delights

NextK

3

4

5

ProductID

PioductName

Suppllei ID

CategorylD

Quanti tyPerUnit

UnhPiice

UnilslnStock

UnitsOn Order

ReorderLevel

Discontin

led

6

4

Chef Anton's Cajun Seasoning

2

2

48 - 6 oz jars

22

53

0

0

FALSE

7

5

Chef Anton's Gumbo Mix

2

2

36 boxes

21.35

0

0 0

0

TRUE

8

65

Louisiana Fiery Hot Pepper Sauce

2

2

32 - 8 oz bottles

21.05

76

0

FALSE

9

66

Louisiana Hot Spiced Okra

2

2

24 - 8 oz jars

17

4

100

20

FALSE

10

11

12

Figure 7.26. Displaying related data on a worksheet

Figure 7.26. Displaying related data on a worksheet

0 0

Post a comment