Viewer Methods

When working with the CrystalReportViewer, we have a number of methods available to us, which will allow us to integrate specific viewer functions into our application. As we move through this section, keep in mind that these methods can be used to create your own look and feel for the report preview window.

Create a new Web Form, which we'll call web_viewer_methods. Again, the code for this application is included with the download code. Drag a CrystalReportViewer onto this form. Include the report product_listing_bytype.rpt in your project (in the download code, the path is Crystal.NET2 0 03/ Chapter05/product_listing_bytype.rpt). Drag a ReportDocument component from the Toolbox onto your form, and when the dialog opens up, select web_viewer_methods.product_listing_bytype from the drop-down box. Click OK.

Now we add some code to tie our report to the application. In the Page_Init event in the designer generated code, once again add:


Now all that remains is to set the ReportSource property in the Page_Load sub:

CrystalReportViewer1.ReportSource = cachedproduct_listing_bytype1

Compile and run this. Now, we're all set to customize our viewer.

In this example, we are actually going to walk through building a custom viewer. The first thing we need to do is set the DisplayToolbar property and the DisplayGroupTree property to False in the Properties pane for the viewer, and add some additional buttons and textboxes to our Web Form using the screen shot earlier as a guide, which we will walk through later in this article.

As we walk through this example, we are going to add the code behind these buttons and this form using the methods described later in this Chapter and learn how to match the viewer user interface to your own application.

Setting Browser Rendering

The CrystalReportViewerBase class provides a number of key properties, one of which is the ClientTarget. The ClientTarget property is a string and determines how the Crystal Report Viewer will render the report.

These strings are:

□ uplevel—for most other Web browsers

□ downlevel—for very basic Web browsers

A Web browser is considered uplevel if it can support the following minimum requirements:

□ ECMAScript ([Script, JavaScript) version 1.2

□ The Microsoft Document Object Model (MSDOM)

□ Cascading style sheets (CSS)

Browsers that fall into the downlevel category include those browsers that provide support only for HTML version 3.2.

So, to set the browser version you are targeting, you could set the ClientTarget property for your form like this, under the Page_Load subroutine:

CrystalReportViewerl.ClientTarget = "ie4"

There is also an Auto value, which is the default setting and automatically selects the best rendering option based on the browser type. Unless you are writing an application for a specific browser or compatibility level, leaving this property set to Auto will provide the best viewing experience for the browser you are using.

For more information on detecting the browser type your Web application is using, see the topic "Detecting Browser Types in Web Forms" in the Visual Studio .NET combined help collection.

Refreshing the Data in a Report

When a report is refreshed, it goes back to the database for the most current set of data available and runs the report again. On our custom Web viewer, you should have a Refresh button, so pull a Button control onto the Web Form and rename it Refresh_Button in the ID property in the Properties pane. Change the text property to Refresh.

Now, double-click the Refresh_Button on your form to open the code for it. We can add some code behind this button to refresh the report using the RefreshReport method as shown subsequently:

Private Sub Refresh_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Refresh_Button.Click CrystalReportViewer1.RefreshReport() End Sub

Compile and run the application. The button should now be present on your form. Click it. This will cause the report to return to the database and read the records again. Use this functionality with caution; if a report has a large SQL query to perform before it can return the first page, you may experience performance problems.

Page Navigation and Zoom

Now we are going to insert some buttons across the top of our Web Form in the same way we did with the Refresh button, with the following names and text values:

Button Name (ID Property Value)

Text Property Value









We access these properties, once again, through the Properties pane in Visual Studio .NET.

For page navigation using the buttons we have drawn on our custom form, we have a number of methods that can be called without any additional parameters, as shown:

□ ShowFirstPage

□ ShowPreviousPage

□ ShowNextPage

□ ShowLastPage

These methods do not return a result, and unlike the Windows Forms Viewer, the Web Form Viewer does not have a GetCurrentPageNumber method, which would have returned an integer representing the page you are currently viewing.

To add these methods to the page navigation buttons, double-click the appropriate buttons on your Web Form and enter the code behind, as shown:


Do this for the other three buttons, including the appropriate method for each. Compile the project and test these buttons.

In addition to page navigation, you also have the ability to choose the zoom factor that is applied to your report. By default, the zoom is set to 100% of the report size unless you specify otherwise.

In our custom viewer, you should have a drop-down list for the zoom factor. To create our own zoom factor functionality, drag a drop-down list onto the form. Open the properties for your drop-down list (in our example, we have named the drop-down list ZoomList).

In the properties for your drop-down list, locate and open the Items property, which should open the dialog shown in Figure 5-20.

Using this dialog, we are going to create the items that will appear in our drop-down list and specify the corresponding values that will be passed to the form when an item is selected. Use the Add button to add items and make sure that the values correspond to the text you have entered (for instance, Full Size = 100, 50% = 50, and so on).

Once you have entered all of the values, click OK to accept these changes and return to your form's design. To use the Zoom method, double-click your drop-down box and add the following code:


This is simply calling the Zoom method using the item the user selects from your drop-down box. When you run your application and preview your custom viewer, you should be able to select your own zoom factor, and have it appear in the browser by pressing the Refresh button, as shown in Figure 5-21.

Figure 5-20
String Picture Crystal Report

Searching Within a Report

Another powerful navigation feature can be found in the SearchForText method within Crystal Reports .NET, which will allow you to search for a specific string that appears in your report.

On our custom viewer, we are going to create a textbox and a button labeled Search. We are going to use this textbox to enter some search string and when the user clicks the Search button, we are going to use the SearchForText method to find the search string within our report.

To start, we will call our textbox TextBox_SearchString and our Search button Search_Button. Add these to the design view of our Web Form, remembering to replace the Text property for the button with Search.

To use the SearchForText method, double-click the Search button and add the following code behind:

Private Sub Search_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search_Button.Click If TextBox_SearchString.Text <> "" Then

CrystalReportViewer1.SearchForText(TextBox_SearchString.Text, _ CrystalDecisions.[Shared].SearchDirection.Forward) End If

TextBox_SearchString.Text = " "

End Sub

The Crystal Report Viewer will search the entire report and when the value is found, go directly to the page on which it appears (the last line of the preceding code is just to clear the textbox for the next search). This method can be called repeatedly to find all of the occurrences of a particular string. Each time it finds the string in your report (in our example that follows, we searched for Youth Helmet), it will jump to that page, as shown in Figure 5-22.

You may have noticed that this method is slightly different between the Windows and Web viewers for Crystal Reports; with both types of Forms Viewer, you can pass the additional parameter of Search Direction for searching forward or backward through your report. However, this method in Windows will also highlight the found value. The Web version does not have this capability.

Was this article helpful?

0 0

Post a comment