WinForm using ReportViewer to make reports

Source: Internet
Author: User

Previously used Crystal Reports feel a bit of trouble, so try to use Microsoft's own report.

The first method is to place the ReportViewer interface on the WinForm interface, the relevant code is as follows:

public DataTable DT;

private void Formreport_load (object sender, EventArgs e)
{
String spath = "D:\\BZJ\\MYBOOKS\\MYBOOKS\\REPORT1.RDLC";

This.reportViewer1.LocalReport.ReportPath = spath;

Microsoft.Reporting.WinForms.ReportDataSource ReportDataSource2 = new

Microsoft.Reporting.WinForms.ReportDataSource ("Dataset1_datatable1", DT);

REPORTVIEWER1.LOCALREPORT.DATASOURCES.ADD (REPORTDATASOURCE2);


This.reportViewer1.RefreshReport ();
}

Description

The DT is passed into the Formreport during initialization, as follows:

ConfigFile m_configfile = new ConfigFile ();

int ilength = M_configfile.m_ibooknamelength;

System.Data.DataTable dt = new System.Data.DataTable ();
Dt. Columns.Add ("Name");
Dt. Columns.Add ("Code");
Dt. Columns.Add ("VIP");

for (int i = 0; i < ListView1.Items.Count; i++)
{
String sName = "";
String sCode = "";


String str = Listview1.items[i]. SUBITEMS[2]. Text;

if (GetLength (str) > Ilength)
{
SName = (getsubstring (str, 0, ilength));
}
Else
{
SName = (str);
}

SCode = (Listview1.items[i]. SUBITEMS[1]. Text);

DataRow dr = dt. NewRow ();
Dr[0] = SName;
DR[1] = SCode;
DR[2] = textboxname.text+ "(" +textboxid.text+ ")";
Dt. Rows.Add (DR);
}

Formreport mformreport = new Formreport ();
MFORMREPORT.DT = DT;


Mformreport.showdialog ();

Of course, the preparations that need to be done before include:

1 Creating a DataSet file

Create a DataSet file (*.xsd) by adding new item data datasets from the series Command: Project right-click menu

Add a DataTable to it and add column

2 Creating a DataSet file

Create a RDLC file (*.RDLC) by adding a new item reporting report by using the series command: Project right-click menu

You need to specify the XSD file created in the data source for 1 in this file

Then, you can design the report.

The second method (does not need to display the report control, directly with the code to finish printing)

Of course, still need to design a good rdlc file, the relevant code is as follows:

private int m_currentpageindex;

<summary>
Declares a list of stream objects to hold the output data for the report, and the Render method of the LocalReport object outputs the report as multiple stream objects.
</summary>
Private ilist<stream> M_streams;

private void Print3 ()
{
System.Data.DataTable dt = getdatatable ();
ReportViewer Rvdoc = new ReportViewer ();
String spath = String. Format ("{0}\\REPORT1.RDLC", System.Windows.Forms.Application.StartupPath);
RvDoc.LocalReport.ReportPath = spath;//plus the path to the report
RVDOC.LOCALREPORT.DATASOURCES.ADD (New Microsoft.Reporting.WinForms.ReportDataSource ("Dataset1_datatable1", DT));
PrintStream (Rvdoc.localreport);
}

Private System.Data.DataTable getdatatable ()
{
ConfigFile m_configfile = new ConfigFile ();

int ilength = M_configfile.m_ibooknamelength;

System.Data.DataTable dt = new System.Data.DataTable ();

Dt. Columns.Add ("Name");
Dt. Columns.Add ("Code");
Dt. Columns.Add ("VIP");

for (int i = 0; i < ListView1.Items.Count; i++)
{
String sName = "";
String sCode = "";


String str = Listview1.items[i]. SUBITEMS[2]. Text;

if (GetLength (str) > Ilength)
{
SName = (getsubstring (str, 0, ilength));
}
Else
{
SName = (str);
}

SCode = (Listview1.items[i]. SUBITEMS[1]. Text);


DataRow dr = dt. NewRow ();
Dr[0] = SName;
DR[1] = SCode;
DR[2] = Textboxname.text + "(" + Textboxid.text + ")";
Dt. Rows.Add (DR);
}

return DT;
}

Private Stream CreateStream (string name, String filenameextension, Encoding Encoding, String mimeType, bool Willseek)
{
If you need to save the report output data as a file, use the FileStream object.
Stream stream = new MemoryStream ();
M_streams. ADD (stream);
return stream;
}

public void PrintStream (LocalReport rvdoc)
{


Export (Rvdoc);
Printsetting ();
Dispose ();
}
private void Export (LocalReport report)
{
String deviceinfo =
"<DeviceInfo>" +
"<OutputFormat>EMF</OutputFormat>" +
"<PageWidth>3in</PageWidth>" +
"<PageHeight>20in</PageHeight>" +
"<MarginTop>0.1in</MarginTop>" +
"<MarginLeft>0in</MarginLeft>" +
"<MarginRight>0in</MarginRight>" +
"<MarginBottom>0.1in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
M_streams = new list<stream> ();
Outputs the contents of the report to the stream provided by the CreateStream function in the format specified by DeviceInfo.
Report. Render ("Image", DeviceInfo, CreateStream, out warnings);
foreach (Stream stream in M_streams)
Stream. Position = 0;
}

private void Printsetting ()
{
if (M_streams = = NULL | | m_streams. Count = = 0)
throw new Exception ("Error: No print traffic detected");
Declaring a PrintDocument object for printing data
PrintDocument Printdoc = new PrintDocument ();
Get the manifest printer name for the configuration file
System.Configuration.AppSettingsReader appSettings = new System.Configuration.AppSettingsReader ();
1111printdoc.printersettings.printername = Appsettings.getvalue ("Qdprint", Type.GetType ("System.String")). ToString ();
Printdoc.printcontroller = new System.Drawing.Printing.StandardPrintController ();//Specifies that the printer does not display page numbers
Determine if the specified printer is available
if (!printdoc.printersettings.isvalid)
{
throw new Exception ("Error: Printer not found");
}
Else
{

Declares the PrintPage event for the PrintDocument object, and the specific print operation needs to be handled in this event.
Printdoc.printpage + = new Printpageeventhandler (PrintPage);
M_currentpageindex = 0;
Set the number of copies of printer print
PrintDoc.PrinterSettings.Copies = 1;
To perform a print operation, the Print method triggers the PrintPage event.
Printdoc.print ();
}
}
private void PrintPage (object sender, PrintPageEventArgs ev)
{
The metafile object is used to hold graphics in EMF or WMF format.
We have previously exported the contents of the report as data streams in the EMF graphics format.
Metafile pageimage = new Metafile (M_streams[m_currentpageindex]);
Specifies whether to print horizontally
Ev. Pagesettings.landscape = false;



Ev. Graphics.DrawImage (pageimage, 0, 0);

M_streams[m_currentpageindex]. Close ();

Prepare the next page, determined that the operation has not ended
m_currentpageindex++;

Set whether you want to continue printing
Ev. HasMorePages = (M_currentpageindex < M_streams. Count);
}

public void Dispose ()
{
if (m_streams! = null)
{
foreach (Stream stream in M_streams)
Stream. Close ();
M_streams = null;
}
}

The following articles are referenced:

Http://www.cnblogs.com/junjie94wan/archive/2013/09/24/3337364.html

http://blog.163.com/xu_shuhao/blog/static/52577487201072284619646/

Http://www.cnblogs.com/ljx2012/p/4093474.html

WinForm using ReportViewer to make reports

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.