vs2010水晶报表应用
(1)在VS2010环境下添加新项---水晶报表---建立水晶报表---过程:连接数据库按向导设计报表
(2)建Form1存放打印和打印预览按钮
打印预览按钮代码:
private void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.id = 2;
f2.Show();
}
打印按钮代码:
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123;database=test;");
conn.Open();
CrystalReportViewer view = new CrystalReportViewer();
ReportDocument doc = new ReportDocument();
SqlDataAdapter da = new SqlDataAdapter("select * from person where id>=2",conn);
DataTable dt = new DataTable();
da.Fill(dt);
doc.FileName = @"..\..\CrystalReport1.rpt";
doc.SetDataSource(dt);
view.ReportSource = doc;
view.PrintReport();
conn.Close();
打印预览窗体Form2
(1)拖一个报表预览控件到Form2窗体crystalReportView1
Form2的Load事件
public int id;
private void Form2_Load(object sender, EventArgs e)
{
ReportDocument doc = new ReportDocument();
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123;database=test;");
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from person where id>="+id.ToString(),conn);
DataTable dt = new DataTable();
da.Fill(dt);
doc.FileName = @"..\..\CrystalReport1.rpt";
doc.SetDataSource(dt);
crystalReportViewer1.ReportSource = doc;
conn.Close();
}
项目app.config
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
项目属性中的---目标框架改为: net framework 4
如果新建项中没有水晶报表,则安装CRforVS_13_0_12
工具栏中没有要拖放的水晶报表控件工具,则右K添加项就是了