使用快捷键打印现有报表查看器

时间:2021-11-14 05:52:24

I have a Windows form with Report Viewer in it. Currently, I am only able to print using Print icon available on top of the form. I have set the Report to an .rdlc file in design time for Report Viewer control. This is my code in Load event of a form:

我有一个带有Report Viewer的Windows表单。目前,我只能使用表单顶部的“打印”图标进行打印。我已在报表查看器控件的设计时将报表设置为.rdlc文件。这是我在表单的Load事件中的代码:

private void BillViewer_Load(object sender, EventArgs e)
        {
            reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
            reportViewer1.ZoomMode = ZoomMode.PageWidth;
            DataTable tempDt = CafePOS.Menu.Model.MenuModel.GetOrderSummary(table_reservation_id);
            DataTable billDt = new DataTable();
            billDt.Columns.Add("menu_item_name");
            billDt.Columns.Add("menu_rate");
            billDt.Columns.Add("qty");
            billDt.Columns.Add("total");
            int numOfCols = 0;

            foreach (DataRow dr in tempDt.Rows)
            {
                billDt.Rows.Add(dr["menu_item_name"], (dr["menu_rate"]), (dr["qty"]), (dr["total"]));
                numOfCols++;
            }
            billDataDT = billDt;
            //calculate total
            decimal totalCal = 0;
            foreach (DataRow drr in tempDt.Rows)
            {
                totalCal += decimal.Parse(drr["total"].ToString());
            }
            total = totalCal.ToString();
            grandTotal = grandTotal.ToString();
            discount = discount.ToString();
            received = "0.00";
            returnAmt = "0.00";
            Main.CommonDataSet.bill_dtDataTable DSbillDt = new Main.CommonDataSet.bill_dtDataTable();
            DSbillDt.Clear();
            IDataReader idr = DSbillDt.CreateDataReader();
            DSbillDt.Load(idr, LoadOption.OverwriteChanges);
            ReportDataSource rds = new ReportDataSource("CDS", billDataDT);
            reportViewer1.LocalReport.DataSources[0] = rds;
            List<ReportParameter> rpc = new List<ReportParameter>();
            rpc.Add(new ReportParameter("billDate", DateTime.Now.ToShortDateString()));
            rpc.Add(new ReportParameter("companyName", Library.GlobalValue.company_name));
            rpc.Add(new ReportParameter("companyAddress", Library.GlobalValue.address));
            rpc.Add(new ReportParameter("companyPhone", Library.GlobalValue.phone));
            rpc.Add(new ReportParameter("total", total));
            rpc.Add(new ReportParameter("grandTotal", grandTotal));
            rpc.Add(new ReportParameter("discount", discount));
            rpc.Add(new ReportParameter("cashReceived", received));
            rpc.Add(new ReportParameter("cashReturned", returnAmt));
            reportViewer1.LocalReport.SetParameters(rpc);
            this.reportViewer1.RefreshReport();
        }

private void BillViewer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.P)
            {
                //print report
            }
        }

On KeyDown event, I want to print the Report that is loaded in ReportViewer control. How can I do so? Thanks in advance.

在KeyDown事件上,我想打印在ReportViewer控件中加载的Report。我怎么能这样做?提前致谢。

1 个解决方案

#1


2  

protected override void OnKeyDown(KeyEventArgs e)
{
  base.OnKeyDown(e);

  if ((e.Key == Key.P) && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
    reportViewer.PrintDialog();
}

#1


2  

protected override void OnKeyDown(KeyEventArgs e)
{
  base.OnKeyDown(e);

  if ((e.Key == Key.P) && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
    reportViewer.PrintDialog();
}