C#创建无数据源水晶报表极简教程(PUSH模式)
有人说我写东西比较啰嗦,呵呵,这次简单点。
先建个水晶报表工程
创建完后,先把报表界面关了,新建个dataset
在里面创建个表,mytable
根据需要定义几个字段
然后设计报表,选数据源
报表如下
在窗体里写上代码
1 private void Form1_Load(object sender, EventArgs e)
2 {
3 //填充dataset,这里就可以承接任何接口,数据库,数组啥得都行
4 DataSet1 ds1 =new DataSet1();
5 ds1.mytable.AddmytableRow("a1","a2","a3","a4","a5",1,1,1,1,1);
6 ds1.mytable.AddmytableRow("b1","b2","b3","b4","b5",1,1,1,1,1);
7 ds1.mytable.AddmytableRow("c1","c2","c3","c4","c5",1,1,1,1,1);
8 ds1.mytable.AddmytableRow("d1","d2","d3","d4","d5",1,1,1,1,1);
9 ds1.mytable.AddmytableRow("e1","e2","e3","e4","e5",1,1,1,1,1);
10
11 CrystalReport1 cr1 =new CrystalReport1();
12 cr1.SetDataSource (ds1);
13 crystalReportViewer1.ReportSource = cr1;
14 }
2 {
3 //填充dataset,这里就可以承接任何接口,数据库,数组啥得都行
4 DataSet1 ds1 =new DataSet1();
5 ds1.mytable.AddmytableRow("a1","a2","a3","a4","a5",1,1,1,1,1);
6 ds1.mytable.AddmytableRow("b1","b2","b3","b4","b5",1,1,1,1,1);
7 ds1.mytable.AddmytableRow("c1","c2","c3","c4","c5",1,1,1,1,1);
8 ds1.mytable.AddmytableRow("d1","d2","d3","d4","d5",1,1,1,1,1);
9 ds1.mytable.AddmytableRow("e1","e2","e3","e4","e5",1,1,1,1,1);
10
11 CrystalReport1 cr1 =new CrystalReport1();
12 cr1.SetDataSource (ds1);
13 crystalReportViewer1.ReportSource = cr1;
14 }
这就完成了~~
PS1
可以配合 http://www.cnblogs.com/babyt/archive/2008/01/03/1024941.html 实现更灵活的效果
PS220080917: 补充一下一个基础的数据库读取,替换文中的自填充ds段:
SqlConnection conn = new SqlConnection("server=xxx.;Uid=sa;Pwd=;database=test");
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select a1,b1,c1,d1,e1 from test", conn);
DataSet ds = new DataSet();
da.Fill(ds1, "mytable"); //特别注意此处的表名,需与设计时的表名一致
conn.Close();