如何在printPreviewDialog中查看水晶报表中的报表c#

时间:2018-10-03 07:21:36

标签: c# crystal-reports

我在水晶报告中做了报告。当单击按钮时,它将直接打印报告。实际上,我想在出现打印对话框之前在printPreviewDialog中查看报告。而且我不知道如何将Crystal报表连接到printPreviewDialog,以及如何仅显示报表而不在Crystal报表中显示其他边框和窗口。
我在下面输入了代码。

private void button1_Click(object sender, EventArgs e)
{
    CrystalReport1 cr = new CrystalReport1();
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectDatabase.Properties.Settings.MyFirstDBConnectionString"].ToString();
    //Sql query
    string sql = "SELECT * from SaleInfoView";
    DataSet ds = new DataSet();
    SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
    adapter.Fill(ds, "SalesInfoView");
    DataTable dt = ds.Tables["SalesInfoView"];
    cr.SetDataSource(ds.Tables["SalesInfoView"]);
    crystalReportViewer1.ReportSource = cr;
    crystalReportViewer1.Refresh();
    PrintDialog dialog1 = new PrintDialog();
    dialog1.AllowSomePages = true;
    dialog1.AllowPrintToFile = false;

    if (dialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
        int copies = dialog1.PrinterSettings.Copies;
        int fromPage = dialog1.PrinterSettings.FromPage;
        int toPage = dialog1.PrinterSettings.ToPage;
        bool collate = dialog1.PrinterSettings.Collate;

        cr.PrintOptions.PrinterName = dialog1.PrinterSettings.PrinterName;
        cr.PrintToPrinter(copies, collate, fromPage, toPage);
    }
    cr.Dispose();
    dialog1.Dispose();
}

我还已经在下面提交了结果屏幕。 enter image description here

0 个答案:

没有答案