熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

VC# .Net中使用Crystal Report(二)

2022-06-13   來源: .NET編程 

  雙擊button添加button_Click點擊事件

  private void button_Click(object sender SystemEventArgs e)
  {
   try
   {
    if(openFileDialogShowDialog()==DialogResultOK)
    thiscrystalReportViewerReportSource = @openFileDialogFileName;
    //加載水晶報表將報表文件綁定到CrystalReportView 控件;
   }
   catch(Exception error)
   {
    MessageBoxShow(errorToString()錯誤);
   }
  }

  OK!按Ctrl+F運行吧

  可以浏覽你系統內現有的報表實例

  \Program Files\Microsoft Visual Studio NET\Crystal Reports\Samples\Reports\Feature Examples\Chartrpt


  using System;
  using SystemDrawing;
  using SystemCollections;
  using SystemComponentModel;
  using SystemWindowsForms;
  using SystemData;
  namespace WindowsApplication
  {
   /// <summary>
   /// Form 的摘要說明
   /// </summary>
   public class Form : SystemWindowsFormsForm
   {
    private CrystalDecisionsWindowsFormsCrystalReportViewer crystalReportViewer;
    private SystemWindowsFormsButton button;
    private SystemWindowsFormsOpenFileDialog openFileDialog;
    /// <summary>
    /// 必需的設計器變量
    /// </summary>
    private SystemComponentModelContainer components = null;
    public Form()
    {
     //
     // Windows 窗體設計器支持所必需的
     //
     InitializeComponent();
     //
     // TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
     //
    }
    /// <summary>
    /// 清理所有正在使用的資源
    /// </summary>
    protected override void Dispose( bool disposing )
    {
     if( disposing )
     {
      if (components != null)
      {
       componentsDispose();
      }
     }
     baseDispose( disposing );
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// 設計器支持所需的方法 不要使用代碼編輯器修改
    /// 此方法的內容
    /// </summary>
    private void InitializeComponent()
    {
     thiscrystalReportViewer = new
     CrystalDecisionsWindowsFormsCrystalReportViewer();
     thisbutton = new SystemWindowsFormsButton();
     thisopenFileDialog = new SystemWindowsFormsOpenFileDialog();
     thisSuspendLayout();
     //
     // crystalReportViewer
     //
     thiscrystalReportViewerActiveViewIndex = ;
     thiscrystalReportViewerDock= SystemWindowsFormsDockStyleFill;
     thiscrystalReportViewerDockPaddingBottom = ;
     thiscrystalReportViewerDockPaddingLeft = ;
     thiscrystalReportViewerDockPaddingRight = ;
     thiscrystalReportViewerDockPaddingTop = ;
     thiscrystalReportViewerName = crystalReportViewer;
     thiscrystalReportViewerReportSource = null;
     thiscrystalReportViewerSize = new SystemDrawingSize( );
     thiscrystalReportViewerTabIndex = ;
     //
     // button
     //
     thisbuttonAnchor = SystemWindowsFormsAnchorStylesBottom;
     thisbuttonLocation = new SystemDrawingPoint( );
     thisbuttonName = button;
     thisbuttonTabIndex = ;
     thisbuttonText = 打開報表;
     thisbuttonClick += new SystemEventHandler(thisbutton_Click);
     //
     // openFileDialog
     //
     thisopenFileDialogFilter = Crystal Report (*rpt)|*rpt|所有文件(**)|**;
     thisopenFileDialogTitle = 打開水晶報表;
     //
     // Form
     //
     thisAutoScaleBaseSize = new SystemDrawingSize( );
     thisClientSize = new SystemDrawingSize( );
     thisControlsAddRange(new SystemWindowsFormsControl[] {
     thisbutton
     thiscrystalReportViewer});
     thisName = Form;
     thisStartPosition = SystemWindowsFormsFormStartPositionCenterScreen;
     thisText = 水晶報表浏覽器;
     thisResumeLayout(false);
    }
    #endregion
    /// <summary>
    /// 應用程序的主入口點
    /// </summary>
    [STAThread]
    static void Main()
    {
     ApplicationRun(new Form());
    }
    private void button_Click(object sender SystemEventArgs e)
    {
     try
     {
      if(openFileDialogShowDialog()==DialogResultOK)
      thiscrystalReportViewerReportSource = @openFileDialogFileName;
      //加載水晶報表將資源報表綁定到水晶報表查看器
     }
     catch(Exception error)
     {
      MessageBoxShow(errorToString()錯誤); //處理異常錯誤
     }
    }
   }
  }


From:http://tw.wingwit.com/Article/program/net/201311/14646.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.