2017-02-22 151 views
0

我是新來的c#,目前正在製作POS應用程序女巫可以打印收據。我使用reportviewer組件創建收據。我正在工作。但我無法直接傳遞打印命令。當它是預覽我必須按Print Button manually.,但我需要自動打印without a preview.這裏是我的代碼的開始。有沒有什麼辦法與PrintDocument或我怎麼能自動打印此reportviewer綁定這個repotviewr如何在沒有預覽的情況下打印Reportviewer

打開FormReceipt

DateTime thisDay = DateTime.Today; 
      FormReceipt frmReceipt = new FormReceipt(order, String.Format("{0:n}", totalAmmount), String.Format("{0:n}", paidammount), String.Format("{0:n}",change), thisDay.ToString("g"), discount.ToString()); 
      frmReceipt.ShowDialog(); 

設置參數和綁定源

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 
using System.Drawing.Printing; 
using Microsoft.Reporting.WinForms; 
namespace Pos 
{ 
    public partial class FormReceipt : MetroFramework.Forms.MetroForm 
    { 

     List<Receipt> _list; 
     string _total, _cash, _change, _date, _user, _discount; 
     public FormReceipt(List<Receipt> datasource, string total,string cash, string change, string date, string discount) 
     { 
      InitializeComponent(); 

      _list = datasource; 
      _total = total; 
      _cash = cash; 
      _change = change; 
      _date = date; 
      _user = Sessiondata.user; 
      _discount = discount; 
     } 

     private void FormReceipt_Load(object sender, EventArgs e) 
     { 
      ReceiptBindingSource.DataSource = _list; 
      Microsoft.Reporting.WinForms.ReportParameter[] para = new Microsoft.Reporting.WinForms.ReportParameter[]{ 
      new Microsoft.Reporting.WinForms.ReportParameter("pTotal",_total), 
      new Microsoft.Reporting.WinForms.ReportParameter("pCash",_cash), 
      new Microsoft.Reporting.WinForms.ReportParameter("pChange",_change), 
      new Microsoft.Reporting.WinForms.ReportParameter("pDate",_date), 
      new Microsoft.Reporting.WinForms.ReportParameter("pUser",_user), 
      new Microsoft.Reporting.WinForms.ReportParameter("pItems",_list.Count.ToString()), 
      new Microsoft.Reporting.WinForms.ReportParameter("pDiscount",_discount+"%") 
      }; 

      this.reportViewer1.LocalReport.SetParameters(para); 
      this.reportViewer1.RefreshReport(); 


     } 

     private void reportViewer1_Load(object sender, EventArgs e) 
     { 

     } 
    } 
} 

enter image description here

回答

0

我發佈我的代碼工作正常,因爲你想要的。檢查此代碼並根據需要在代碼中進行自定義。

List<Receipt> _list; 
    string _total, _cash, _change, _date, _user, _discount; 
    public FormReceipt(List<Receipt> datasource, string total,string cash, string change, string date, string discount) 
    { 
     InitializeComponent(); 

     _list = datasource; 
     _total = total; 
     _cash = cash; 
     _change = change; 
     _date = date; 
     _user = Sessiondata.user; 
     _discount = discount; 
    } 

    private void FormReceipt_Load(object sender, EventArgs e) 
    { 
     ReceiptBindingSource.DataSource = _list; 
     Microsoft.Reporting.WinForms.ReportParameter[] para = new Microsoft.Reporting.WinForms.ReportParameter[]{ 
     new Microsoft.Reporting.WinForms.ReportParameter("pTotal",_total), 
     new Microsoft.Reporting.WinForms.ReportParameter("pCash",_cash), 
     new Microsoft.Reporting.WinForms.ReportParameter("pChange",_change), 
     new Microsoft.Reporting.WinForms.ReportParameter("pDate",_date), 
     new Microsoft.Reporting.WinForms.ReportParameter("pUser",_user), 
     new Microsoft.Reporting.WinForms.ReportParameter("pItems",_list.Count.ToString()), 
     new Microsoft.Reporting.WinForms.ReportParameter("pDiscount",_discount+"%") 
     }; 

     this.reportViewer1.LocalReport.SetParameters(para); 
     this.reportViewer1.RefreshReport(); 

      Export(ReportViewer1.LocalReport, false); 
      Print(); 
      Dispose(); 
    } 
private int m_currentPageIndex; 
private IList<Stream> 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, bool isLandscape) 
    { 
    string deviceInfo = string.Empty; 
    if (isLandscape) 
    { 
     deviceInfo = 
      @"<DeviceInfo> 
      <OutputFormat>EMF</OutputFormat> 
      <PageWidth>11in</PageWidth> 
      <PageHeight>8.5in</PageHeight> 
      <MarginTop>0.25in</MarginTop> 
      <MarginLeft>0.25in</MarginLeft> 
      <MarginRight>0.25in</MarginRight> 
      <MarginBottom>0.25in</MarginBottom> 
     </DeviceInfo>"; 
    } 
    else 
    { 
     deviceInfo = 
     @"<DeviceInfo> 
      <OutputFormat>EMF</OutputFormat> 
      <PageWidth>8.5in</PageWidth> 
      <PageHeight>11in</PageHeight> 
      <MarginTop>0.25in</MarginTop> 
      <MarginLeft>0.25in</MarginLeft> 
      <MarginRight>0.25in</MarginRight> 
      <MarginBottom>0.25in</MarginBottom> 
     </DeviceInfo>"; 
    } 
    Warning[] warnings; 
    m_streams = new List<Stream>(); 

    // Create Report DataSource 
    ReportDataSource rds = new ReportDataSource(); 

    rds.Value = _list; 

    ReportViewer1.LocalReport.DataSources.Clear(); 
    ReportViewer1.LocalReport.DataSources.Add(rds); 

    report.Render("Image", deviceInfo, CreateStream, 
     out warnings); 
    foreach (Stream stream in m_streams) 
     stream.Position = 0; 
} 
private void Print() 
{ 
    PrinterSettings settings = new PrinterSettings(); //set printer settings 
    string printerName = settings.PrinterName; //use default printer name 

    if (m_streams == null || m_streams.Count == 0) 
     throw new Exception("Error: no stream to print."); 
    PrintDocument printDoc = new PrintDocument(); 

    if (!printDoc.PrinterSettings.IsValid) 
    { 

     Response.Write("<script>alert('" + printerName + "')</script>"); 
    } 
    else 
    { 
     printDoc.PrintPage += new PrintPageEventHandler(PrintPage); 
     m_currentPageIndex = 0; 
     printDoc.Print(); 
    } 

} 
private void PrintPage(object sender, PrintPageEventArgs ev) 
{ 
    Metafile pageImage = new 
     Metafile(m_streams[m_currentPageIndex]); 
    ev.Graphics.DrawImage(pageImage, ev.PageBounds); 
    m_currentPageIndex++; 
    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; 
    } 
} 
相關問題