Use RDLC Report definition files, ReportViewer controls to generate local reports in Vs2010winform projects

Source: Internet
Author: User
Tags define local mdb database visual studio 2010
This is a simple example of generating a local report using the RDLC report definition file, the ReportViewer control. (This article is flawed, edited in 2011.10.29)
First, create a new. Net4.0 Project: (The. NET Framework 2.0 of the following figure is changed to the. NET Framework 4.0----2011.10.29)


The project is named Reportapp and creates two new folders Reports, datasets for the project. The Reports folder stores the report definition file, datasets stores the dataset.


Ii. Creating a DataSet
1. The first dataset: Make sure that when Datasets is selected in Solution Explorer, click "Items" in the menu, "Add New Items", select "Data" on the left, and then select "DataSet"; named Firstdataset.xsd, as shown below.


Create a new connection to the Northwind.mdb database in Server Explorer and add the Northwind.mdb database to your project.


Drag the Products table in the Northwind.mdb to the firstdataset.xsd.
2, the same method to create a second Dataset:SecondDataset.xsd. Drag the Orders table in Northwind.mdb to it.
Third, create reports
1. The first report reportproducts: When reports is selected in Solution Explorer, click Project in the menu, add New Item, select Reporting on the left, and then select Report ; named REPORTPRODUCTS.RDLC, as the following figure.


Modify the copy to output directory in the REPORTPRODUCTS.RDLC file properties instead: Always replicate. If you do not modify this property, the report definition file will not be found when you generate the report below.
Select the report data toolbox on the left to create a new dataset.


The name of the




named DataSet is: Rpproductsdataset, which is useful for subsequent code. Select the data source for Firstdataset and select the available data set for products.
Drag a table to the report in the Toolbox, set the Tablix1 datasetname to Rpproductsdataset, and then set the fields for each cell in the table.


2, use the same method to establish a second report: REPORTORDERS.RDLC, the report's dataset name is Rpordersdataset.


Four, generate report
Double-hit open Form1.vb, drag the ReportViewer control from the Toolbox to the Form1. If the ReportViewer control is not found in VS2010, you can select the ReportViewer control by right-clicking the data in the toolbox and selecting Select Item .... Note: There are two kinds of ReportViewer controls for WinForm and Web editions. The
sets the anchor property of the ReportViewer control to top, Bottom, left, and right. The Form1 WindowState property is set to maximized.
Drag two button from the toolbox, named Reportproducts, Reportorders, respectively. The code for the


Form1 is as follows:
 

Public Class Form1 ' Private Sub form1_load (sender as System.Object, E as System.EventArgs) Handles MyBase.Load ' Me.ReportViewer1.RefreshReport () ' End Sub ' reportproducts report Private Sub button1_click (sender as Sys Tem. Object, E as System.EventArgs) Handles Button1.Click ' define DataSet Dim Productsds as New firstdataset ' data SQL command Dim commandtext As String = "SELECT * FROM Products" ' report definition file Dim rdlcfile As String = ' Repor TS/REPORTPRODUCTS.RDLC the dataset name used in the "' Report Dim Reportds as String =" rpproductsdataset "' Generate report Me . Getreport (Productsds, CommandText, Rdlcfile, Reportds, False) End Sub ' reportorders report Private Sub Button  2_click (sender as System.Object, E as System.EventArgs) Handles Button2.click ' define DataSet Dim Productsds as New Seconddataset ' Data SQL command Dim commandtext As String = ' select ' From Orders ' report definition file Dim rdl CFile as String = "REPORTS/REPORTORDERS.RDLC the dataset name used in the report, note that the case sensitivity Dim Reportds as String = "Rpordersdataset" generated in the name Report Me.getreport (Productsds, CommandText, Rdlcfile, Reportds, True) End Sub Private Sub Getreport (ByVal DS as DataSet, ByVal CommandText as String, ByVal Rdlcfile as String, ByVal Reportds as String, ByVal Islandscape as Boole
        An) ' DS: Use which DataSet ' CommandText: Data SQL command ' RDLCFILE:RDLC file path and file name ' REPORTDS:RDLC report file use dataset name ' Islandscape: Whether the report page displays '----------------------------get data----------------------------------' definition in landscape
        According to the set ' Dim datasetused As New DS ' defines the connection of the Dim conn As New Oledb.oledbconnection ' connection to the keyword obtained from the configuration Conn.
        ConnectionString = My.Settings.NorthwindConnectionString ' Define data command Dim Comm as New Data.OleDb.OleDbCommand Comm.commandtype = CommandType.Text Comm.commandtext = CommandText comm.
     Connection = Conn ' Define data adapter   Dim DA as New oledb.oledbdataadapter (comm) ' Populates the data Using Conn Conn. Open () DA. Fill (ds. Tables (0)) end using '--------------------------------Generate report-----------------------------' setting uses local Report Mode Me.ReportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local ' Empty ReportViewer1 control Original data source Me.ReportViewer1.LocalReport.DataSources.Clear () ' Define local report variable Dim reportengine as MICROSOFT.R Eporting. Winforms.localreport reportengine = Reportviewer1.localreport ' Specify report file Reportengine.reportpath = RD
        Lcfile ' Loads the data source, note: The case of the dataset is sensitive. REPORTENGINE.DATASOURCES.ADD (New Microsoft.Reporting.WinForms.ReportDataSource (Reportds, DS). Tables (0)) '---------------------set preview---------------------------------' display mode is Print preview mode Me.repo Rtviewer1.setdisplaymode (Microsoft.Reporting.WinForms.DisplayMode.PrintLayout) ' page Set variable Dim pageset as Drawing.Printing.PageSettings = Me.ReportViewer1.GetPageSettings ' page is horizontal display pageset. Landscape = Islandscape ' Set page Me.ReportViewer1.SetPageSettings (Pageset) ' is displayed as a percentage me.reportv Iewer1.
  
        Zoommode = Microsoft.Reporting.WinForms.ZoomMode.Percent ' 100% ratio Me.ReportViewer1.ZoomPercent = 100 ' Refresh report Me.ReportViewer1.RefreshReport () End Sub End Class




The final effect is as follows:



PostScript: About deployment of RDLC reports

The ReportViewer control is not part of the. NET Framework framework. If you are publishing reports, you need to install Microsoft Report Viewer Redistributable Package (download address: http://www.microsoft.com/downloads/zh-cn/ DETAILS.ASPX?FAMILYID=A941C6B2-64DD-4D03-9CA7-4017A0D164FD&DISPLAYLANG=ZH-CN), which is used to view controls for reports that are designed with Microsoft report technology. When you publish your application, the project-> properties-> publish-> Prerequisites Check the Microsoft Visual Studio 2010 Report Viewer.


Http://www.360doc.com/content/12/0411/11/9587654_202709089.shtml

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.