///
<summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form4_Load(object sender, EventArgs e)
{
//班级信息加载
string sql1 = "select bno,bname from class";
DataSet ds1 = DB.GetDataSet(sql1);
comboBox1.DataSource = ds1.Tables[0].DefaultView;
comboBox1.DisplayMember = "bname";
comboBox1.ValueMember = "bno";
//课程信息加载
string sql2 = "select cno,cname from course";
DataSet
ds2 = DB.GetDataSet(sql2);
comboBox2.DataSource = ds2.Tables[0].DefaultView;
comboBox2.DisplayMember = "cname";
comboBox2.ValueMember = "cno";
}
|
///
<summary>
/// 准备打开水晶报表文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
object bno = this.comboBox1.SelectedValue;
object cno = this.comboBox2.SelectedValue;
string s = "select scno from sc where cno=" + cno.ToString() + " and bno=" + bno.ToString();
//查询是否选择的课程编号和班级编号有成绩信息
if (DB.GetdataRow("select scno from sc where cno=" + cno.ToString() + " and bno=" + bno.ToString()) > 0)
{
Form1 f = new Form1(cno, bno);
//重载Show方法,打开form1时候,传递cno 课程号和 bno 班级号参数过去
f.Show();
}
else
{
MessageBox.Show("该班级该课程无成绩信息!");
}
}
|
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
CrystalDecisions.Shared;
using
CrystalDecisions.CrystalReports.Engine;
//注意:必须增加水晶报表的这两个命名空间才可以在编程状态下操作水晶报表对象。
namespace
WindowsApplication1
{
public partial class Form1 : Form
{
Buessiness business = new Buessiness();//实例化业务逻辑层,具体方法在下面分述介绍
Mydatabase db = new Mydatabase();//实例数据库层,具体方法在ADO.NET已有介绍
//此处的重载,接收来自外部的系统参数,课程号cno和班级号bno
public Form1(object cno,object bno)
{
InitializeComponent();
//初始化界面各个参数
int percent_qm, percent_sj, percent_ps, ctime;
//获取表头三项成绩百分比。
business.GetCoursecheck(Convert.ToInt32(cno), Convert.ToInt32(bno), out percent_qm, out percent_sj, out percent_ps);
//第一个业务逻辑层的方法,请见下面的介绍。
string xqmess, cname, zy, nj, bname, tname, year1, month1, day1, clx_string;
Int16 clx_num;
//获取表头标题各项信息
business.GetPaperMessage(Convert.ToInt32(cno), Convert.ToInt32(bno), 2, out xqmess, out cname, out zy, out nj, out bname, out tname, out year1, out month1, out day1, out clx_num, out clx_string, out ctime);
//第二个业务逻辑层的方法,请见下面的介绍。
//获取表尾统计信息
int first, second, third, forth, fifth, total1;
business.GetPjStatistics(Convert.ToInt32(cno), Convert.ToInt32(bno), out first, out second, out third, out forth, out fifth, out total1);
//第三个业务逻辑层的方法,请见下面的介绍。
//注意学习如何获取磁盘内的水晶报表的rpt文件
string path = Environment.CurrentDirectory + "\\CrystalReport1.rpt";
ReportDocument report = new ReportDocument();
report.Load(path);//加载报表
((TextObject)report.ReportDefinition.ReportObjects["Text_title1"]).Text = xqmess;
((TextObject)report.ReportDefinition.ReportObjects["Text_cname"]).Text = cname;
((TextObject)report.ReportDefinition.ReportObjects["Text3"]).Text = zy;
((TextObject)report.ReportDefinition.ReportObjects["Text5"]).Text = bname;
//下面是表尾部分:
((TextObject)report.ReportDefinition.ReportObjects["Text_yx"]).Text = first.ToString();
((TextObject)report.ReportDefinition.ReportObjects["Text_lh"]).Text = second.ToString();
((TextObject)report.ReportDefinition.ReportObjects["Text_zd"]).Text = third.ToString();
((TextObject)report.ReportDefinition.ReportObjects["Text_jg"]).Text = forth.ToString();
((TextObject)report.ReportDefinition.ReportObjects["Text_nojg"]).Text = fifth.ToString();
((TextObject)report.ReportDefinition.ReportObjects["Text_totalnum"]).Text = total1.ToString();
//下面是显示标头各个分值的百分比
((TextObject)report.ReportDefinition.ReportObjects["Text19"]).Text = percent_ps.ToString();
((TextObject)report.ReportDefinition.ReportObjects["Text21"]).Text = percent_sj.ToString();
((TextObject)report.ReportDefinition.ReportObjects["Text25"]).Text = percent_qm.ToString();
//下面是写年月日信息
((TextObject)report.ReportDefinition.ReportObjects["Text38"]).Text = year1.ToString();
((TextObject)report.ReportDefinition.ReportObjects["Text39"]).Text = month1.ToString();
((TextObject)report.ReportDefinition.ReportObjects["Text40"]).Text = day1.ToString();
((TextObject)report.ReportDefinition.ReportObjects["Text43"]).Text = year1.ToString();
((TextObject)report.ReportDefinition.ReportObjects["Text42"]).Text = month1.ToString();
((TextObject)report.ReportDefinition.ReportObjects["Text41"]).Text = day1.ToString();
//下面开始动态加载SQL脚本,此次是通过dataset加载水晶报表
DataSet Ds = db.GetDataSet("select snumber,sname,Grade,Grade_ps,Grade_end,Grade_design from sc where cno=" + Convert.ToString(cno) + " and bno=" + Convert.ToString(bno));
report.SetDataSource(Ds.Tables[0]);
//最后绑定数据源,不写无法显示哦^o^
this.crystalReportViewer1.ReportSource = report;
}
}
}
|
本文出自 “熊猫写程序” 博客,转载请与作者联系!