Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Getting Started with Crystal Reports for Visual Studio 2005 : Page 3

Learn how Crystal Reports and Visual Studio 2005 combine to help streamline one of the most common development tasks: creating reports in business applications.


advertisement
Previewing a Report
After the report is created, you can preview how it will look like by clicking the Main Report Preview button located at the bottom of the report. The report will be shown in two columns—the left column shows a list of companies that you can select and the right shows the report with the selected company's name shown highlighted in blue.

Viewing a Report in a Windows Form
A report is only useful if it can be displayed in a Windows form during runtime. To do that, you will need to use the CrystalReportViewer control, a control that displays the report in a page-based layout.

First, add a new Windows Form to the project and use its default name of Form2. Double-click on the CrystalReportViewer control (located in the Toolbox) to add a new instance onto Form2 (see Figure 6). By default, the CrystalReportViewer control will fill the entire form. If you do not want it to fill the entire form, simply set the Dock property of the control to either left, right, top, bottom, or none. For this example, leave it as it is.



Figure 6. Form2: Adding the CrystalReportViewer control to Form2.

Back to Form1, add a Button control to it and set is Text property to View Report. Name the control btnViewReport. Double-click the button and code its Click event handler as follows:

Private Sub btnViewReport_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnViewReport.Click Dim report As New CrystalReport1 With Form2 .CrystalReportViewer1.ReportSource = report .ShowDialog() End With End Sub

Press F5 to test the application. When you click the View Report button, Form2 will appear, displaying the report (see Figure 7).

Figure 7. Displaying Form2: This image displays the report in runtime.

At the top of Form2, there are several controls associated with the report:

  • Export Report: Saves the report in various file formats (.rpt, .pdf, .xls, .doc, .rtf, etc.)
  • Print Report: Prints the report to the printer.
  • Refresh Report: Refreshes the report.
  • Toggle Group Tree: Hides/displays the left column of the report.
  • Navigational Buttons: Navigates between pages in the report.
  • Find Text: Performs a search for specific words.
  • Zoom: Adjusts the view proportion of the report.
Adding Parameters to the Report
The report created in the previous section lists out all the customers' orders. This might not be too useful as most of the time you might only want to view the orders for a particular customer. Hence, you should modify the report so that during runtime you can specify the particular customer to list.

In the Main Report view of the report, right-click on the Parameter Fields items located in the Field Explorer and select New…. This will create a new parameter for your report so that you can pass it a value during runtime.

In the Create Parameter Field dialog, specify Customer_ID as the name and use the other default values. Click OK. In the Choose Field dialog, choose CustomerID and click OK.

Click the Select Expert button ocated in the toolbar of Visual Studio 2005.

In the Select Expert dialog, set the values of the controls as shown in Figure 8. This indicates that the customer ID used for the report will be dependent on the values passed into the Customer_ID parameter (represented as {?Customer_ID}).


Figure 8. Setting Values: Setting the value of the CustomerID to be dependent on the parameter.
 
Figure 9. Adding Controls: Adding the controls to Form1.

Back in Form1, add a Label and ComboBox control as shown in Figure 9.

Switch to the code-behind of Form1 and import the following namespaces:

Imports System.Data Imports System.Data.SqlClient

In the Form1_Load event, code the following to populate the ComboBox control with all the customers' ID:

Private Sub Form1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim connStr As String = _ "Data Source=.\SQLEXPRESS;" & _ "Initial Catalog=Northwind;" & _ "Integrated Security=True" Dim sql As String = "SELECT CustomerID FROM Customers" Dim conn As SqlConnection = New SqlConnection(connStr) Dim comm As SqlCommand = New SqlCommand(sql, conn) conn.Open() Dim reader As SqlDataReader = comm.ExecuteReader While reader.Read ComboBox1.Items.Add(reader(0)) End While conn.Close() End Sub

Figure 10. Parameters Added: Viewing the orders made by the selected customer.

Modify the Click event of the button control to set the parameter with a value (which is the customer ID selected in the ComboBox control):

Private Sub btnViewReport_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnViewReport.Click Dim report As New CrystalReport1 report.SetParameterValue( _ "Customer_ID", ComboBox1.Text) With Form2 .CrystalReportViewer1.ReportSource = report .ShowDialog() End With End Sub

That's it! Press F5 to test the application. When the form is loaded, select a customer ID and click the View Report button. The report will now only contain the orders made by the selected customer (see Figure 10).



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap