winform使用水晶報表的例子

來源:互聯網
上載者:User
水晶報表 ///Form檔案
namespace SimpleApp
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.WinForms;
using System.Data;

public class SimpleForm : System.WinForms.Form
{
    private System.ComponentModel.Container components;
    private System.WinForms.Button btnSelectReport;    
    private SeagateSoftware.WinForms.CrystalReportViewer crystalReportViewer1;

    public SimpleForm()
    {
        //
        // Required for Win Form Designer support
        //
        InitializeComponent();
        EnableViewer(false);
    }

    public override void Dispose()
    {
        base.Dispose();
        components.Dispose();
    }

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.crystalReportViewer1 = new SeagateSoftware.WinForms.CrystalReportViewer();
        this.btnSelectReport = new System.WinForms.Button();
        
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.Text = "Viewing a Crystal Report";
        this.StartPosition = System.WinForms.FormStartPosition.CenterScreen;
        //@design this.TrayLargeIcon = false;
        //@design this.TrayHeight = 0;
        this.ClientSize = new System.Drawing.Size(664, 509);
        
        crystalReportViewer1.Location = new System.Drawing.Point(8, 40);
        crystalReportViewer1.Size = new System.Drawing.Size(648, 464);
        crystalReportViewer1.ActiveViewIndex = -1;
        crystalReportViewer1.SelectionFormula = "";
        crystalReportViewer1.DisplayToolbar = true;
        crystalReportViewer1.DisplayGroupTree = true;
        crystalReportViewer1.TabIndex = 2;
        crystalReportViewer1.Anchor = System.WinForms.AnchorStyles.All;
        crystalReportViewer1.DisplayBackgroundEdge = false;
        
        btnSelectReport.Location = new System.Drawing.Point(8, 8);
        btnSelectReport.Size = new System.Drawing.Size(96, 24);
        btnSelectReport.TabIndex = 1;
        btnSelectReport.Font = new System.Drawing.Font("Arial", 8f, System.Drawing.FontStyle.Bold);
        btnSelectReport.Text = "Select Report";
        btnSelectReport.Click += new System.EventHandler(btnSelectReport_Click);
        
        this.Controls.Add(btnSelectReport);
        this.Controls.Add(crystalReportViewer1);
        
    }
    protected void btnSelectReport_Click(object sender, System.EventArgs e)
    {
        // Get a RPT file to display in the crystal report viewer
        OpenFileDialog openFileDlg = new OpenFileDialog();
        openFileDlg.InitialDirectory = Utils.GetSampleReportsDir();
        openFileDlg.Filter = "Crystal Reports (*.rpt)|*.rpt|All Files (*.*)|*.*";        
        if (openFileDlg.ShowDialog() == DialogResult.OK)
        {
            // Set the ReportName property
            crystalReportViewer1.ReportName = openFileDlg.FileName;    
            EnableViewer(true);
        }
    }

    // This function enables and disables the Crystal Reports Win Form Viewer
    private void EnableViewer(bool show)
    {        
        SeagateSoftware.WinForms.ToolbarStateChangeEvent evt = new SeagateSoftware.WinForms.ToolbarStateChangeEvent(show);
        crystalReportViewer1.Toolbar.OnStateChange(this, evt);
    }
    /*
     * The main entry point for the application.
     *
     */
    public static void Main(string[] args)
    {
        Application.Run(new SimpleForm());
    }
}
}

///Util.cs
namespace SimpleApp
{
using System;
using Microsoft.Win32;

/// <summary>
///    Summary description for Utils.
/// </summary>
public class Utils
{
    public static string GetSampleReportsDir()
    {
        // Get the path VS was installed to
        RegistryKey regKey = Registry.LocalMachine;    
        regKey = regKey.OpenSubKey("Software\\Microsoft\\VisualStudio\\7.0\\Setup\\VS");
        string dir = regKey.GetValue("ProductDir").ToString();
        dir += "Crystal Reports\\Samples\\Reports\\";
        return dir;
    }
}
}


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。