水晶报表如何动态绑定数据?急求代码?

bluenight911 2003-08-23 07:59:52
现在正在做一个查询学生成绩的Winform.现在form上添加了一个datagrid和一个crystalreportview,根据要求查询学生成绩记录并显示在datagrid中已实现,现在的想法是水晶报表也要实现得到和datagrid中相同的数据,即运行以后datagrid和cristalreport动态绑定同一个dataset,这儿绑定的是dataview.即datagrid显示什么数据,crystalreportview就显示什么数据.然后再在crystalreportview中把表打印出来.打印再且不说,动态绑定dataset就花了我不少时间.
主要代码如下:
private void SearcherBk_Load(object sender, System.EventArgs e)
{
dbCon=new dbConnect();
dbCon.Open();
BkMage=new BkMag(dbCon);
DataSet DS=new DataSet();
SqlDataAdapter DA=new SqlDataAdapter();
TheRpt=new CrystalReport2();
//TheRpt.SetDataSource(BkMage.GetDataView());
crystalReportViewer2.ReportSource=TheRpt;
DA.SelectCommand=new SqlCommand("select distinct 专业名 from ZyDm",dbCon.GetDbCon() );
DA.Fill(DS,"zym");
string a;
DataRow DR;
for (int i=0;i<DS.Tables["zym"].Rows.Count;i++)
{
DR=DS.Tables["zym"].Rows[i];
a=DR[0].ToString();
cbZym.Items.Add(a);
cbZym.Text=a;


}
//cbZym.DataSource=DS.Tables["zym"];
//cbZym.DisplayMember ="专业名" ; //cbZym.ValueMember="专业名"


DA.SelectCommand=new SqlCommand("select distinct 开课学期 from KeC",dbCon.GetDbCon() );
DS.Clear();
//DataSet DT=new DataSet();;
DA.Fill(DS,"xq");
cbXq.DataSource =DS.Tables["xq" ];
cbXq.DisplayMember="开课学期";
cbXh.Enabled =false;
cbXh.Text="";


}

private void button1_Click(object sender, System.EventArgs e)
{
if(cbXh.Enabled==false)
BkMage.SetSeleCmd(cbZym.Text ,cbXq.Text,"",true );
else
BkMage.SetSeleCmd(cbZym.Text,cbXq.Text,cbXh.Text,false);
BkInfo.DataSource=BkMage.GetDataView();
TheRpt.SetDataSource(BkMage.GetDataView());
this.crystalReportViewer2.ReportSource=TheRpt;
//----------------------------------------------------------------------//
this.crystalReportViewer2.RefreshReport();
if (BkMage.GetDataView().Count==0 )
MessageBox.Show("当前无任何记录!","信息");
}
当程序执行到this.crystalReportViewer2.ReportSource=TheRpt;时,出错,显示为

未处理的“CrystalDecisions.CrystalReports.Engine.InvalidArgumentException”类型的异常出现在 crystaldecisions.crystalreports.engine.dll 中。

其他信息: 文件 C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\temp_4d10433c-5944-4f81-be03-581347fcb397.rpt 内出错:
无效表号。

BkMage.cs为代码剥离出来的类.
请高手们帮忙,马上要交差了.最好帮我指出错误并更正一下,我很赖的哦.
...全文
206 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluenight911 2003-08-26
  • 打赏
  • 举报
回复
未处理的“CrystalDecisions.CrystalReports.Engine.InvalidArgumentException”类型的异常出现在 crystaldecisions.crystalreports.engine.dll 中。

其他信息: 文件 C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\temp_4d10433c-5944-4f81-be03-581347fcb397.rpt 内出错:
无效表号。
那这个错误出在哪儿?
bluenight911 2003-08-26
  • 打赏
  • 举报
回复
zhongwanli(想一下,就会可以)说的我都做了呀,dataset也已建了呀,只不过是沿用了一下填充datagrid的dataset而已,难道一定要再建一个dataset1.xsd.顺便补充一下,现在用的dataset
手写的,而不是通过数据集dataset1.xsd来完成的.
zhongwanli 2003-08-26
  • 打赏
  • 举报
回复
先声明一个CrystalReport 的对象和DataSet 对象

用SqlDataAdapter 的fill 方法填充DataSet 后

将Dataset 绑定到CrystalReport ,用SetDataSource()方法

即可,如果你想再绑定到查看器上

crystalreportviewer1.ReportSource=CrystalReport对象
好运 2003-08-26
  • 打赏
  • 举报
回复
DataSet是一定要建的,报表的列一顶是固定的,内容可以随便的填充,但报表的样子是必须先确定的
bluenight911 2003-08-24
  • 打赏
  • 举报
回复
public class SearcherBk : System.Windows.Forms.Form
{
private BkMag BkMage;
dbConnect dbCon;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.DataGrid BkInfo;
private System.Windows.Forms.ComboBox cbXq;
private System.Windows.Forms.ComboBox cbZym;
private System.Windows.Forms.RadioButton rdXh;
private System.Windows.Forms.RadioButton rdXhAll;
private System.Windows.Forms.ComboBox cbXh;
private System.Windows.Forms.Button btSearch;
private CrystalReport2 TheRpt;
private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer2;
已声明.
Ernestvivi 2003-08-23
  • 打赏
  • 举报
回复
TheRpt=new CrystalReport2();???
bluenight911 2003-08-23
  • 打赏
  • 举报
回复
是否还要再建一个dataset1.xsd ,ADO.NET 数据集对象.把有关的几张表拖进去,然后再把要显示的字段绑定到水晶报表.好像没这个必要吧,因为已有了一个dataset.

110,529

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧