如何实现水晶报表和DataGrid的动态绑定?急啊!马上要交付了.

bluenight911 2003-08-22 11:49:24
在学生管理系统的成绩查询这一Windows的form中,成绩查询功能已实现,可显示在DataGrid表中.查询过程是跟据班级,开课学期,课程名为条件实现查询,查询结果显示在form中的DataGrid控件中.现在我在这个form中添加一个CrystalReportViewer1,然后在项目中新建一个水晶报表,为空表.本意是想把显示在Datagrid中的表数据也显示在水晶报表中,然后把数据打印出来.源码如下.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;
namespace AdultMag
{
/// <summary>
/// SearcherCj 的摘要说明。
/// </summary>
public class SearcherCj : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox txtXH;
private System.Windows.Forms.RadioButton radioONE;
private System.Windows.Forms.RadioButton radioALL;
private System.Windows.Forms.Button bnQuery;
private System.Windows.Forms.Button bnPrint;
private System.Windows.Forms.DataGrid dataGridCJCX;
private DataView dv;
private DataAccess access;
bool flag;
private System.Windows.Forms.Button bnAll;
private System.Windows.Forms.ComboBox comboXQ;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtKC;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ComboBox comboBJ;
private System.Windows.Forms.Label label1;
private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
//public ReportDocument TheRpt=null;

public SearcherCj()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//this.crystalReportViewer1.DataBind();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//主要代码


-------------------------------------------------
access=new DataAccess();
dv=access.GetCJDataView();
dataGridCJCX.DataSource=dv;
CrystalReport1 TheRpt=new CrystalReport1();
TheRpt.SetDataSource(dataGridCJCX.DataSource);
this.crystalReportViewer1.ReportSource=TheRpt;
//this.crystalReportViewer1.DataBind();
TheRpt.Refresh();
-------------------------------------------------
}
由于帖子内容太长,后面内容略!
关键代码在两虚线之间.运行是老提示
未处理的“CrystalDecisions.CrystalReports.Engine.InvalidArgumentException”类型的异常出现在 crystaldecisions.crystalreports.engine.dll 中。

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

我想请问错在哪儿,并且如何实现我想要的功能.即把DataGrid的数据也显示在水晶报表中.然后打印出来.实现水晶报表的动态绑定.
...全文
61 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lemong 2003-08-22
  • 打赏
  • 举报
回复
注意:
生成空水晶报表的sql语句,应该与运行时的sql结构一致,即所取的字段类型、长度必须一致
lemong 2003-08-22
  • 打赏
  • 举报
回复
水晶报表动态绑定不能用空表!
绑定时,程序监测报表的结构和dataset是否相符合,不符合就会出现上述错误。
应该用向导生成预制得水晶报表(包含数据结构),可以用空得sql语句生成dataset,在设计期绑定,得到所谓的空报表。
在运行时,只要更改dataset,或者,重新fill dataset ,就可以了
bluenight911 2003-08-22
  • 打赏
  • 举报
回复
代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;
namespace AdultMag
{
/// <summary>
/// SearcherCj 的摘要说明。
/// </summary>
public class SearcherCj : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox txtXH;
private System.Windows.Forms.RadioButton radioONE;
private System.Windows.Forms.RadioButton radioALL;
private System.Windows.Forms.Button bnQuery;
private System.Windows.Forms.Button bnPrint;
private System.Windows.Forms.DataGrid dataGridCJCX;
private DataView dv;
private DataAccess access;
bool flag;
private CrystalReport1 TheRpt;
private System.Windows.Forms.Button bnAll;
private System.Windows.Forms.ComboBox comboXQ;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtKC;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ComboBox comboBJ;
private System.Windows.Forms.Label label1;
private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
//public ReportDocument TheRpt=null;

public SearcherCj()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//this.crystalReportViewer1.DataBind();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
DataAccess access=new DataAccess();
dv=access.GetCJDataView();
dataGridCJCX.DataSource=dv;
TheRpt=new CrystalReport1();
TheRpt.SetDataSource(dv);
crystalReportViewer1.ReportSource=TheRpt;
//this.crystalReportViewer1.DataBind();
}

..............


private void bnAll_Click(object sender, System.EventArgs e)
{
dv=access.GetCJDataView();
dataGridCJCX.DataSource=dv;
TheRpt.SetDataSource(dv);
this.crystalReportViewer1.ReportSource=TheRpt;
}
}
}
还是不能实现.
bluenight911 2003-08-22
  • 打赏
  • 举报
回复
我又用了另一种方法,即按照生成DataGrid相同的方法来生成水晶报表.从同一个Dataset里读出数据并同时显示在DataGrid和水晶报表查看器里.可是还不行.如lemong(風之影)所说的如何来生成空的水晶报表的sql语句,我试了一下,如果用sql语句生成,其效果还是和字段绑定一样的,在查看器中显示的还是数据库中整个没经过删选的记录.另外我不懂这句话的意思:应该用向导生成预制得水晶报表(包含数据结构),可以用空得sql语句生成dataset,在设计期绑定,得到所谓的空报表。
既然通过sql语句生成,那报表已绑定好了字段,如何还能叫空的报表????
milchcow 2003-08-22
  • 打赏
  • 举报
回复
不能直接绑定DATAGRID,水晶报表不支持动态列
bluenight911 2003-08-22
  • 打赏
  • 举报
回复
另外我是对多张表进行查询生成操作.
bluenight911 2003-08-22
  • 打赏
  • 举报
回复
查询所得的DataGrid的字段和记录都不是固定的,字段和记录都按查询条件的不同而不同.直接绑定DataGrid不行吗?一定要建一个Dataset吗?在查询生成DataGrid的过程中已运用sql语句.DataGrid中已经可以显示查询结果.是不是还要对水晶报表进行一次sql语句的操作?

110,526

社区成员

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

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

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