C# 如何使用c sharp编程调用Microsoft report Viewer的打印按钮

C# 如何使用c sharp编程调用Microsoft report Viewer的打印按钮,c#,.net,reportviewer,C#,.net,Reportviewer,如何使用c夏普代码调用报表查看器的打印按钮 以编程方式打印报告,而不首先向用户显示预览。您只需指定打印设备的名称,报告将自动打印,无需用户干预。示例代码如下:。但我需要在报表查看器控件上显示.rdlc文件,并使用自定义按钮打印该报表文件。如何解决此问题?@user1146956:您不必取消按钮或查看器控件。但是,如果您想在没有用户干预的情况下打印报告,那么您应该按照上面的链接建议执行。不要尝试使用代码单击按钮;那是行不通的,谢谢。任何使用此代码的人请进行此小更改。1.将IList更新为IList

如何使用c夏普代码调用报表查看器的打印按钮


以编程方式打印报告,而不首先向用户显示预览。您只需指定打印设备的名称,报告将自动打印,无需用户干预。示例代码如下:。

但我需要在报表查看器控件上显示.rdlc文件,并使用自定义按钮打印该报表文件。如何解决此问题?@user1146956:您不必取消按钮或查看器控件。但是,如果您想在没有用户干预的情况下打印报告,那么您应该按照上面的链接建议执行。不要尝试使用代码单击按钮;那是行不通的,谢谢。任何使用此代码的人请进行此小更改。1.将
IList
更新为
IList
2。更新
m_streams=new List()
m_streams=新列表()3。更新
m_currentPageIndex m_streams.Count
m_currentPageIndex
Ref:Microsoft教程演练:打印本地报告而不预览
directly copy this code in a class called "Printing" and Call the "Run" method with your reportviewer name as parameter. Example obj.Run(reportviewer1);

////////////////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;


class Printing
{
private int m_currentPageIndex;
private IList m_streams;


    private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
    {
        Stream stream = new MemoryStream();
        m_streams.Add(stream);
        return stream;
    }

    private void Export(LocalReport report)
    {
        string deviceInfo =
          @"
            EMF
            8.5in
            11in
            0.25in
            0.25in
            0.25in
            0.25in
        ";
        Warning[] warnings;
        m_streams = new List();
        report.Render("Image", deviceInfo, CreateStream,
           out warnings);
        foreach (Stream stream in m_streams)
            stream.Position = 0;
    }

    private void PrintPage(object sender, PrintPageEventArgs ev)
    {
        Metafile pageImage = new
           Metafile(m_streams[m_currentPageIndex]);

        // Adjust rectangular area with printer margins.
        Rectangle adjustedRect = new Rectangle(
            ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
            ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
            ev.PageBounds.Width,
            ev.PageBounds.Height);

        // Draw a white background for the report
        ev.Graphics.FillRectangle(Brushes.White, adjustedRect);

        // Draw the report content
        ev.Graphics.DrawImage(pageImage, adjustedRect);

        // Prepare for the next page. Make sure we haven't hit the end.
        m_currentPageIndex++;
        ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
    }

    private void Print()
    {
        if (m_streams == null || m_streams.Count == 0)
            throw new Exception("Error: no stream to print.");
        PrintDocument printDoc = new PrintDocument();
        if (!printDoc.PrinterSettings.IsValid)
        {
            throw new Exception("Error: cannot find the default printer.");
        }
        else
        {
            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
            m_currentPageIndex = 0;
            printDoc.Print();
        }
    }

    public void Run(ReportViewer rpt)
    {
        Export(rpt.LocalReport);
        Print();
    }

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