2013-05-23 3 views
2

Я работаю над одним приложением. в DataGridView Я создал событие dataGridView1_CellContentDoubleClick, который бы предполагалось открыть другую форму, которая имеет crystalreportviewer component, затем пройти путь crystalreportdocument к CrystalReportViewer и загрузить отчет, поэтому он может представить данные ...открытая форма с crystalreportsviewer с использованием C#

до сих пор я это сделал :

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 выглядит следующим образом:

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 выглядит следующим образом:

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

ответ

1

Событие:

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

Полный пример кода моих мыслей. Имейте upvote! :) – Flipbed

+0

без проблем m8. Поскольку я разработчик младшего и промежуточного уровня, и я понимаю, как это может быть: иногда частичный код не получает достаточной информации, чтобы понять, как все работает, поэтому вместо отправки частичных данных я добавил полный код, чтобы облегчить для остальных людей. ура. – dovla091

2

Вы создаете новую форму в своем проекте. Затем в коде этой формы вы изменяете конструктор, чтобы принять строковый аргумент, являющийся трактом отчета.

Код, который вы затем использовать, чтобы показать, что форма является:

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

Не уверен, что я понял вас очень хорошо, у меня есть событие, которому нужно передать переменную (путь к кристальному документу), и из этого мне нужно открыть другую форму (очевидно, это часть) Form3 form = new Form(); form.Show(); но в этой форме3 мне нужно загрузить crystalreportviewer и пройти путь к файлу crystalreport, чтобы зритель мог открыть отчет ... – dovla091

+0

Насколько я понял, у вас есть компонент просмотра Crystal Report в этой форме? Так что единственное, что вам нужно, это путь? В этом случае измените конструктор для Form3, чтобы включить строковый параметр, который вы можете использовать, чтобы получить путь, который будет загружать зритель Crystal Report. – Flipbed

+0

Да, у формы есть компонент crystalReportViewer1: после того, как я создал форму, я добавил компонент drag-drop crystalreportviewer ... Я добавил дополнительный код, чтобы увидеть, что я сделал до сих пор ... – dovla091

Смежные вопросы