C# 使用C使用crystalreportsviewer打开表单#

C# 使用C使用crystalreportsviewer打开表单#,c#,datagridview,C#,Datagridview,我正在处理一个应用程序。在datagridview中,我创建了eventdataGridView1\u CellContentDoubleClick,它应该打开另一个包含crystalreportviewer组件的窗体,然后将crystalreportdocument的路径传递给crystalreportviewer并加载该报表,以便它可以显示数据 到目前为止,我已经做到了: private void dataGridView1_CellContentDoubleClick(object

我正在处理一个应用程序。在datagridview中,我创建了event
dataGridView1\u CellContentDoubleClick
,它应该打开另一个包含
crystalreportviewer组件的窗体,然后将crystalreportdocument的路径传递给crystalreportviewer并加载该报表,以便它可以显示数据

到目前为止,我已经做到了:

    private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        //string that carries path of the report document
        string path = dataGridView1.SelectedRows[0].Cells["ReportPath"].Value.ToString();

    }
表格3看起来像:

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ICAMReports
{
   public partial class Form3 : Form
   {
        public Form3(string path)
        {
            InitializeComponent();
        }
   }
}
namespace ICAMReports
{
     partial class Form3
     {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {

        this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
        this.SuspendLayout();
        // 
        // crystalReportViewer1
        // 
        this.crystalReportViewer1.ActiveViewIndex = -1;
        this.crystalReportViewer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.crystalReportViewer1.Cursor = System.Windows.Forms.Cursors.Default;
        this.crystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.crystalReportViewer1.Location = new System.Drawing.Point(0, 0);
        this.crystalReportViewer1.Name = "crystalReportViewer1";
        this.crystalReportViewer1.Size = new System.Drawing.Size(609, 413);
        this.crystalReportViewer1.TabIndex = 0;
        // 
        // Form3
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(609, 413);
        this.Controls.Add(this.crystalReportViewer1);
        this.Name = "Form3";
        this.Text = "Form3";
        this.ResumeLayout(false);

    }

    #endregion

    private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
    }
}
表单3.设计器看起来像:

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ICAMReports
{
   public partial class Form3 : Form
   {
        public Form3(string path)
        {
            InitializeComponent();
        }
   }
}
namespace ICAMReports
{
     partial class Form3
     {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {

        this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
        this.SuspendLayout();
        // 
        // crystalReportViewer1
        // 
        this.crystalReportViewer1.ActiveViewIndex = -1;
        this.crystalReportViewer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.crystalReportViewer1.Cursor = System.Windows.Forms.Cursors.Default;
        this.crystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.crystalReportViewer1.Location = new System.Drawing.Point(0, 0);
        this.crystalReportViewer1.Name = "crystalReportViewer1";
        this.crystalReportViewer1.Size = new System.Drawing.Size(609, 413);
        this.crystalReportViewer1.TabIndex = 0;
        // 
        // Form3
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(609, 413);
        this.Controls.Add(this.crystalReportViewer1);
        this.Name = "Form3";
        this.Text = "Form3";
        this.ResumeLayout(false);

    }

    #endregion

    private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
    }
}
命名空间ICAMReports
{
部分类别表格3
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.crystalReportViewer1=新的CrystalDecisions.Windows.Forms.CrystalReportViewer();
这个.SuspendLayout();
// 
//crystalReportViewer1
// 
this.crystalReportViewer1.ActiveViewIndex=-1;
this.crystalReportViewer1.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;
this.crystalReportViewer1.Cursor=System.Windows.Forms.Cursors.Default;
this.crystalReportViewer1.Dock=System.Windows.Forms.DockStyle.Fill;
this.crystalReportViewer1.Location=新系统.Drawing.Point(0,0);
this.crystalReportViewer1.Name=“crystalReportViewer1”;
this.crystalReportViewer1.Size=新系统.Drawing.Size(609413);
this.crystalReportViewer1.TabIndex=0;
// 
//表格3
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(609413);
this.Controls.Add(this.crystalReportViewer1);
this.Name=“Form3”;
this.Text=“Form3”;
此选项为.resume布局(false);
}
#端区
private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
}
}

您可以在项目中创建一个新表单。然后,在该表单的代码中,您将构造函数更改为采用作为报告路径的字符串参数

然后用于显示该表单的代码是:

//CrystalReportForm is your new form.
CrystalReportForm form = new CrystalReportForm(path); 
form.Show(); //or form.ShowDialog();
活动:

表格3.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ICAMReports
{
    public partial class Form3 : Form
    {
        public string source;
        public Form3(string path)
        {
            source = path;
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            this.crystalReportViewer1.ReportSource = source;
        }
    }
}
Form3.Designer.cs

namespace ICAMReports
{
    partial class Form3
    {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
        this.SuspendLayout();
        // 
        // crystalReportViewer1
        // 
        this.crystalReportViewer1.ActiveViewIndex = -1;
        this.crystalReportViewer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.crystalReportViewer1.Cursor = System.Windows.Forms.Cursors.Default;
        this.crystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.crystalReportViewer1.Location = new System.Drawing.Point(0, 0);
        this.crystalReportViewer1.Name = "crystalReportViewer1";
        this.crystalReportViewer1.Size = new System.Drawing.Size(609, 413);
        this.crystalReportViewer1.TabIndex = 0;
        // 
        // Form3
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(609, 413);
        this.Controls.Add(this.crystalReportViewer1);
        this.Name = "Form3";
        this.Text = "Form3";
        this.Load += new System.EventHandler(this.Form3_Load);
        this.ResumeLayout(false);

    }

    #endregion

    private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
}
}
命名空间ICAMReports
{
部分类别表格3
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.crystalReportViewer1=新的CrystalDecisions.Windows.Forms.CrystalReportViewer();
这个.SuspendLayout();
// 
//crystalReportViewer1
// 
this.crystalReportViewer1.ActiveViewIndex=-1;
this.crystalReportViewer1.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;
this.crystalReportViewer1.Cursor=System.Windows.Forms.Cursors.Default;
this.crystalReportViewer1.Dock=System.Windows.Forms.DockStyle.Fill;
this.crystalReportViewer1.Location=新系统.Drawing.Point(0,0);
this.crystalReportViewer1.Name=“crystalReportViewer1”;
this.crystalReportViewer1.Size=新系统.Drawing.Size(609413);
this.crystalReportViewer1.TabIndex=0;
// 
//表格3
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(609413);
this.Controls.Add(this.crystalReportViewer1);
this.Name=“Form3”;
this.Text=“Form3”;
this.Load+=new System.EventHandler(this.Form3\u Load);
此选项为.resume布局(false);
}
#端区
private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
}
}

不确定我是否理解您的意思,我有一个需要传递变量(crystal document path)的事件,我需要从中打开另一个表单(显然就是这个部分)Form3 form=new form();form.Show();但在那张表格3中,我需要加载crystalreportviewer并传递crystalreport文档的路径,以便查看器可以打开报告…据我所知,您已经有了该表格中的crystalreportviewer组件了?所以你唯一需要的就是路径?在这种情况下,请更改Form3的构造函数,使其包含一个字符串参数,您可以使用该参数获取crystal report viewer将加载的路径。是的,表单具有crystalReportViewer1组件:创建表单后,我通过拖放crystalreportviewer组件添加了该组件。。。我已经添加了额外的代码来查看我到目前为止所做的工作……没问题。由于我是初级/中级开发人员,我理解这一点:有时部分代码无法获得足够的信息来理解事情的工作原理,因此我添加了完整的代码,以使其他人更容易理解。干杯