水晶报表(WINFORM)动态添加不同属性,只需要一个Form就可以实现n多张水晶报表的显示兼打印功能
水晶报表(WINFORM)动态添加不同属性,只需要一个Form就可以实现n多张水晶报表的显示兼打印功能,另外需要一个comboBox就行了。分享代码如下
1、对于添加DataSet和Crystalreport在这里就不多说了,详细步骤可以看http://www.cnblogs.com/lvk618/p/3338510.html
2、连接数据库并实例化(全局变量)
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Login;Integrated Security=True"); DataSet1 mydata = new DataSet1(); DataSet2 mydata1 = new DataSet2(); SqlDataAdapter da;
3、主要功能
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)//当选择comboBox上的名称,就会触发,SelectedIndexChanged更改时发生 { string cmb = this.comboBox1.Text; // if (!cmb.Equals(String.Empty)) if (cmb == "CR_push" + ".rpt")//判断 { da = new SqlDataAdapter("SELECT * FROM test1", conn); da.TableMappings.Add("Table", "test1"); da.Fill(mydata, "test1"); CR_push cr = new CR_push(); cr.SetDataSource(mydata); crystalReportViewer1.ReportSource = cr; TextObject text = cr.ReportDefinition.ReportObjects["Text5"] as TextObject; text.Text = "作者:吕坤"; TextObject text1 = cr.ReportDefinition.ReportObjects["Text6"] as TextObject; text1.Text = "备注"; } else if (cmb == "CrystalReport1" + ".rpt") { da = new SqlDataAdapter("SELECT 药品,药品序号,规格,单位代码 FROM INADD", conn); da.TableMappings.Add("Table", "INADD"); da.Fill(mydata1, "INADD"); CrystalReport1 cr1 = new CrystalReport1(); cr1.SetDataSource(mydata1); crystalReportViewer1.ReportSource = cr1; TextObject text = cr1.ReportDefinition.ReportObjects["Text5"] as TextObject; text.Text = "出单员:吕坤"; TextObject text1 = cr1.ReportDefinition.ReportObjects["Text6"] as TextObject; text1.Text = "备注"; } }
4、效果图如下