ReportViewer Example Tutorial (reprint)

Source: Internet
Author: User

This example operation mainly implements

A. Simple rendering of ReportViewer associated REPORT1.RDLC

B. Rendering of REPORT1.RDLC with report parameters

C. Populating a report with a program-generated dataset

D. Calling a stored procedure to generate a dataset to populate a report

==========

Simple rendering

==========

1. Open VS2005, file-new website Select language type (C #)

2. Design the default.aspx that it has generated under this solution

3. Pull the ReportViewer of the Toolbox-data into the Default.aspx design interface

4. When you select ReportViewer1 with the mouse, you can see the small triangle icon in the upper right corner.

5. Click on the small triangle with the mouse to expand the ReportViewer Task shortcut menu

6. Since the project is not yet ready to report, choose to design a new report

7. Click Design new report to render the report REPORT1.RDLC design interface

8. Because the project is not yet well-done web data sources

So now you're adding a new data source

(If the Web site data source interface is not present, you can click the report Design screen,

Options such as "Data", "Report" and more appear in the top menu

You can select Show data Source under the Data menu.

9. Click "Add new Data Source" to appear "TableAdapter Configuration Wizard"

10. Select a data connection (such as the Northwind database attached to this computer) and the next step

11. Select whether you want to save the connection string to the application configuration file, and the next step

12. Select the command type, at which point you can click the Cancel button to exit the wizard

13. Save All

14. Select "Server Explorer" under the View menu to see the data connection you have just established, you can see the tables, views, stored procedures, functions in the Northwind database

15. Locate and open the DataSet1.xsd dataset under the App_Code folder under the solution tree

16. You can see that the Open DataSet Designer is empty, and you can pull the table or stored procedure for Northwind under Server Explorer into the DataSet Designer (for example, by pulling in the Orders table and sales by year for this stored procedure)

17. Save All

18. Double-click REPORT1.RDLC to open the report design screen to pull the table of report items in the report Design Toolbox at this point into the report design interface

19. Select "Show Data Sources" under the "Data" menu,

You can see two tables under DataSet1 under the Web site data source orders and Sales by year

20. Select any of the fields under orders (such as OrderID and CustomerID) at this point

The "Details" section of the table that is pulled to the REPORT1.RDLC design interface is used for report display

21. Save All

22. Go back to the Default.aspx design interface and bind the report to the ReportViewer1

Select ReportViewer1 to display the REPORT1.RDLC you just designed

23. Save all, press F5 or CTRL F5 to see the initial effect

======================================

Next you add a report parameter for REPORT1.RDLC

======================================

24. Return to the design interface of report REPORT1.RDLC

Click "Report Parameters" under the Report menu

25. In the report parameter, in the Settings box, add a report parameter to make the relevant settings and determine to save

(if its parameter name is Rptparaa, the type is string)

26. Pull a text box report item to the REPORT1.RDLC design interface to display the title of the report

27. Select and Right-click the text box and select expression in the pop-up menu.

Go to the Edit Expression dialog box

28. In the Edit Expression dialog box, select the parameter,

and double-click the Rptparaa you just set to make the value of the text box =parameters!rptparaa.value

29. Save changes to the REPORT1.RDLC

30. Because the new REPORT1.RDLC report text box needs to have the report parameter value of the incoming

So when ReportViewer1 to REPORT1.RDLC,

The editor of Default.aspx.cs

31. Add references in Default.aspx.cs

Using Microsoft.Reporting.WebForms;

Add the following code to the Page_Load

ReportParameter Rptparaa = new ReportParameter ("Rptparaa", "test Report Parameters");

ReportViewer1.LocalReport.SetParameters (new reportparameter[] {rptparaa});

32. Save and run to see the effect

=========================================

Next, you need to customize the report data that you want to render

That is, by designing a dataset

Data binding and rendering of reports in accordance with the requirements

=========================================

33. Set ReportViewer1 's visible to False

34. Add a button to the Default.aspx design interface Button1

Access to the database and the data population of the dataset are implemented through the Click event of this button

and data binding to ReportViewer1.

Default.aspx.cs Add a reference

Using System.Data.SqlClient;

Using Microsoft.Reporting.WebForms;

Examples of code in button1_click such as the following

SqlConnection myconn = new SqlConnection ("Data source=192.168.0.36;initial catalog=northwind; User Id=sa; Password=sa ");

SqlDataAdapter Myda = new SqlDataAdapter ("select Top 5 * from Orders", myconn);

DataSet myds = new DataSet ();

MyConn.Open ();

Myda. Fill (myds);

Myconn.close ();

Reportviewer1.visible = true;

ReportParameter Rptparaa = new ReportParameter ("Rptparaa", "test Report Parameters");

ReportViewer1.LocalReport.SetParameters (new reportparameter[] {rptparaa});

ReportDataSource rds = new ReportDataSource ("Dataset1_orders", myDS. Tables[0]);

ReportViewer1.LocalReport.DataSources.Clear ();

REPORTVIEWER1.LOCALREPORT.DATASOURCES.ADD (RDS);

ReportViewer1.LocalReport.Refresh ();

Note ReportDataSource rds = new ReportDataSource ("Dataset1_orders", myDS. Tables[0]);

The "Dataset1_orders" is with the foreground HTML program's

In the Name= "Dataset1_orders" is consistent

You can obtain the required data for report rendering by customizing the SQL statement when the dataset is populated

It is important to note that the structure in the tables[0] of the dataset is ReportViewer1 because the associated report at this time is REPORT1.RDLC

Be sure to include the field that REPORT1.RDLC renders, so the SQL statement here is select Top 5 * FROM Orders

36. Save run click Button1 for report rendering

===============================================

If the data to be rendered by the report originates from the stored procedure

Then when the dataset is loaded, the data from the stored procedure can be

===============================================

37. Right-click the project to add a new item, select the report, add REPORT2.RDLC to the project,

Set its rendered data source as stored procedure Sales by year

The same design interface in REPORT2.RDLC joins the table report item

And the field in the Sales by year table under DataSet1 in the site data source

Pull-in Report Item table to display in detail data

38. Like default.aspx add a new page to the project default2.aspx

Add a button and ReportViewer1 to Default2.aspx

and set ReportViewer1 to render the report as REPORT2.RDLC

Set the visible of ReportViewer1 to False

Double-click the button to write its Click event

Set default2.aspx as the start page for the project

39. Add references in Default2.aspx.cs

Using System.Data.SqlClient;

Using Microsoft.Reporting.WebForms;

An example of the 40.button1_click event code is shown below

protected void Button1_Click (object sender, EventArgs e)

{

SqlConnection myconn = new SqlConnection ("Data source=192.168.0.36;initial catalog=northwind; User Id=sa; Password=sa ");

SqlDataAdapter Myda = new SqlDataAdapter ("Sales by Year", myconn);

Myda. SelectCommand.Parameters.AddWithValue ("@Beginning_Date", "1997-10-10");

Myda. SelectCommand.Parameters.AddWithValue ("@Ending_Date", "2000-10-10");

Myda.SelectCommand.CommandType = CommandType.StoredProcedure;

DataSet myds = new DataSet ();

MyConn.Open ();

Myda. Fill (myds);

Myconn.close ();

Reportviewer1.visible = true;

ReportDataSource rds = new ReportDataSource ("Dataset1_sales_by_year", myDS. Tables[0]);

ReportViewer1.LocalReport.DataSources.Clear ();

REPORTVIEWER1.LOCALREPORT.DATASOURCES.ADD (RDS);

ReportViewer1.LocalReport.Refresh ();

}

41. Save Run Click Button Report Effect view

ReportViewer Example Tutorial (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.