dovla110010101
dovla110010101

Reputation: 431

open form with crystalreportsviewer using C#

I'm working on one application. in datagridview I have created event dataGridView1_CellContentDoubleClick which would supposed to open another form that has crystalreportviewer component, then pass the path of the crystalreportdocument to the crystalreportviewer and load that report so it can present data...

so far I have done this:

    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();

    }

Form3 looks like:

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();
        }
   }
}

Form3.Designer looks like:

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;
    }
}

Upvotes: 2

Views: 2414

Answers (2)

dovla110010101
dovla110010101

Reputation: 431

Event:

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        string path = dataGridView1.SelectedRows[0].Cells["ReportPath"].Value.ToString();
        Form3 form = new Form3(path);
        //ReportDocument crystal = new ReportDocument();
        //crystal.Load(dataGridView1.SelectedRows[0].Cells["ReportPath"].Value.ToString());
        //pass = crystal;
        form.Show();
    }

Form3.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;
}
}

Upvotes: 1

Flipbed
Flipbed

Reputation: 720

You create a new form in your project. Then in that form's code you change the constructor to take a string argument that is the report path.

The code you then use to show that form is:

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

Upvotes: 2

Related Questions