winform使用水晶报表的例子
2024-07-21 02:25:28
供稿:网友
///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;
}
}
}