Tài liệu Print Labels and Control the Order in Which Records Will Be Printed ppt

6 371 0
Tài liệu Print Labels and Control the Order in Which Records Will Be Printed ppt

Đang tải... (xem toàn văn)

Thông tin tài liệu

10.6 Print Labels and Control the Order in Which Records Will Be Printed I need to be able to have my application print labels for my user's customer list. Sometimes my user needs to print labels based on the postal code, and other times he needs to print labels alphabetically by company name. How do I do this at runtime? Technique To accomplish this task, you will use the Report Expert to create mailing labels. You then will use code to update the sort order at run-time. Creating the Mailing Labels To create the mailing labels, you create a new report and choose Mailing Labels for the type of Expert to use. You then fill in the Data tab, which uses Customers for the table, and grab the following fields: CompanyName, ContactName, ContactTitle, Address, Formula (@CityRegionPostal= { Customers.City} & ", " & { Customers.Region} & " "& { Customers.PostalCode} ), and Country. You can see how this will look in Figure 10.22. Figure 10.22. Fields that will be used in the mailing labels. After clicking Next, you are taken to the tab that allows you to choose which type of label you want to use. For this How-To, the Address (Avery 5160) is used. Everything else on the page is chosen for you (see Figure 10.23). Figure 10.23. You have control over how your labels look. Now you can just click Finish, and the final mailing label report is created (see Figure 10.24). Figure 10.24. These appear in nice mailing label format when they're viewed on a form. Controlling the Sort Order at Runtime To control the sort order at runtime, you use properties, methods, and objects of the ReportDocument. Specifically, you traverse down the ReportDocument object model, looking at the Database object and moving down to the Fields level. You do this first when you're loading up a combo box with fields that are being used in the report. The following lines of code accomplish this: For Each dfCurr In Me.rdHowTo10_6.Database.Tables.Item(0).Fields Me.cboSortFields.Items.Add(dfCurr.Name) Next dfCurr is a FieldDefinition object, which means it is a field definition for a given field in the report. The other object that will be used from the Report definition is the DataDefinition object, and the SortFields collection off of that. You can see this with the following lines of code, which set the sorting for the report based on which field they chose in the combo box: With Me.rdHowTo10_6 dfSort = .Database.Tables.Item(0).Fields.Item(Me.cboSortFields.Text) .DataDefinition.SortFields.Item(0).Field = dfSort End With Me.cvwCustomerLabels.RefreshReport() You can see once again that the RefreshReport method is called after updating the SortFields item. Steps Open and run the Visual Basic .NET-Chapter 10 solution. Click on the button labeled How-To 10.6. You can select fields from the ComboBox control on the top of the form, and the report reflects the sorting selection (see Figure 10.25). 1. Create a new Crystal Report. Choose Mail Labels for the Report Expert to use. 2. Fill in the Data tab, choosing Northwind for the database, and Customers for the table to use. 3. Choose the fields as specified by the "Technique" section: CompanyName, ContactName, ContactTitle, Address, Formula (@CityRegionPostal= { Customers.City} & ", " & { Customers.Region} & " " & { Customers.PostalCode} ), and Country. 4. On the Label tab, choose Address (Avery 5160) for the mailing label type. Click Finish. 5. Create a Windows Form. 6. Drag on a ReportDocument object, and set it to point to the report you created in the past few steps. Then name your report document rdHowTo10_6. 7. Place the controls shown in Figure 10.22 onto the form with the properties set forth in Table 10.5. Table 10.5. Label, Combo, and CrystalReportViewer Controls and Their Property Settings Object Property Setting Label Text Pick a Field to Sort ComboBox Name cboSortFields CrystalReportViewer Name cvwCustomerLabels Anchor Top, Bottom, Right, Left ReportSource rdHowTo10_6 8. Add the code in Listing 10.5 to the Load event of the form. As described in the "Technique" section, this code iterates through each of the fields in the table that the report is based on and loads them into the Items collection of cboSortFields. Listing 10.5 frmHowTo10_5.vb: Populating the Selection Combo Box Private Sub frmHowTo10_6_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim dfCurr As CrystalDecisions.CrystalReports.Engine.FieldDefinition '- Iterate through the table that the report is based on ' and load the fields into a combo box. For Each dfCurr In Me.rdHowTo10_6.Database.Tables.Item(0).Fields Me.cboSortFields.Items.Add(dfCurr.Name) Next End Sub 9. Add the code in Listing 10.6 to the SelectedIndexChanged event of cboSortFields. This code takes the selected item from cboSortFields and locates it in the table that the report is based on. The DataDefinition object is retrieved and then assigned to the first items in the SortFields collection. Last, the report is redisplayed using the RefreshReport method. Listing 10.6 frmHowTo10_5.vb: Populating the Selection Combo Box Private Sub cboSortFields_SelectedIndexChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cboSortFields.SelectedIndexChanged Dim dfSort As CrystalDecisions.CrystalReports.Engine.FieldDefinition '- Use the field that is picked in the combo box in ' the SortFields collection. With Me.rdHowTo10_6 dfSort = .Database.Tables.Item(0).Fields.Item(Me.cboSortFields.Text) .DataDefinition.SortFields.Item(0).Field = dfSort End With Me.cvwCustomerLabels.RefreshReport() End Sub Figure 10.25. Let your user sort the mailing labels as needed. Comments If you have more than one field you want to sort on, then you could use the same technique, just adding code such as this: dfSort = .Database.Tables.Item(0).Fields.Item(Me.cboSortFields2.Text) .DataDefinition.SortFields.Item(1).Field = dfSort The 0 is replaced by 1, and you have another combo box from which you must pick the second sort field. . 10.6 Print Labels and Control the Order in Which Records Will Be Printed I need to be able to have my application print labels for my user's. task, you will use the Report Expert to create mailing labels. You then will use code to update the sort order at run-time. Creating the Mailing Labels To

Ngày đăng: 26/01/2014, 11:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan