VS2010 水晶报表的使用方法
程序员文章站
2024-03-02 08:14:04
在vs2010中新建一个“windows 窗体应用程序”项目,在该项目中添加一个水晶报表“crystalreport1.rpt”,然后在项目上点击鼠标右键属性,将“目标框架...
在vs2010中新建一个“windows 窗体应用程序”项目,在该项目中添加一个水晶报表“crystalreport1.rpt”,然后在项目上点击鼠标右键属性,将“目标框架”改为“.net framework 4”
<startup uselegacyv2runtimeactivationpolicy="true">
<supportedruntime version="v4.0" sku=".netframework,version=v4.0"/>
</startup>
private void form1_load(object sender, eventargs e)
{
string connstr = "data source=.\\sqlexpress;initial catalog=dbtest;user id=sa;password=test";
sqlconnection conn = new sqlconnection(connstr);
conn.open();
try
{
string sql = "select * from customer where email!='test@gmail.com'";
sqldataadapter sda = new sqldataadapter(sql, conn);
dataset ds = new dataset();
sda.fill(ds, "tmptable");
打开app.config文件,在“startup”节点一个“uselegacyv2runtimeactivationpolicy="true"”属性
复制代码 代码如下:
<startup uselegacyv2runtimeactivationpolicy="true">
<supportedruntime version="v4.0" sku=".netframework,version=v4.0"/>
</startup>
在form1窗体中,从工具箱拖出一个crystal report viewer控件,双击form窗体,是双击form窗体,不是crystal report viewer,在后台的form_load事件中写入如下代码:
复制代码 代码如下:
private void form1_load(object sender, eventargs e)
{
string connstr = "data source=.\\sqlexpress;initial catalog=dbtest;user id=sa;password=test";
sqlconnection conn = new sqlconnection(connstr);
conn.open();
try
{
string sql = "select * from customer where email!='test@gmail.com'";
sqldataadapter sda = new sqldataadapter(sql, conn);
dataset ds = new dataset();
sda.fill(ds, "tmptable");
string reportpath = system.windows.forms.application.startuppath + @"\crystalreport1.rpt";
reportdocument rd = new reportdocument();
rd.load(reportpath);
rd.setdatasource(ds.tables[0].defaultview);
this.crystalreportviewer1.reportsource = rd;
}
catch (exception ex)
{
throw new exception(ex.message.tostring());
}
finally
{
conn.close();
}
}
这样就ok了