初学水晶报表,一头雾水,都是希望先找个简单的示例型的文章看看,本篇就非常的适合(仅适合接触水晶报表但是一个都没有写过的同仁)。
本示例环境:windows2003、vs.net2003、Sql Server2000、水晶报表为vs.net2003自带的。
示例程序为winform下读取sql server2000数据库中一表,使用水晶报表显示列表,非常简单的一个,下面就开始了。
首先建立数据库,生成表的sql语句如下:
CREATE TABLE [dbo].[test] (
[id] [decimal](18, 0) IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[card] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[createdate] [datetime] NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[test] ADD
CONSTRAINT [PK_test_1] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
GO


打开vs.net2003,新建立一个C#的winform项目。
程序分3个文件,分别为:Form1.cs、DataSetTest.xsd、CrystalReportTest.rpt
Form1.cs:建立项目时已经生成;
DataSetTest.xsd:该文件为数据集文件,建立完成后在文件中点击“服务器资源管理器”,这样在左边出现了带有“数据连接”的菜单,然后试图展开下面的结点时提示需要一些连接数据库的参数,包括选择数据名称,sa用户和密码等,连接成功后展开“表”的结点,选择你建立的表,将表拖到“DataSetTest.xsd”文件中,这样会自动建立了一个表结构的数据集,然后就建立完成了,生成一下项目,如果不生成直接往下做的话在下一步的建立报表文件时就会遇到问题(可以不生成来试一下就明白了)。
CrystalReportTest.rpt:报表文件,在项目中添加CrystalReport文件,添加时会出现建立向导,首先选择“使用报表专家”,“确定”后展开“项目数据”,展开“ADO.NET数据集”,展开“datasettest”,看到了刚才建立的表“test”,双击就添加到了右边的“报表中的表”中,点“下一步”,再点“全部添加”,点“下一步”,然后点“完成”,这样文件已经建立;下面是设计报表的表样,设计的表样如下图:

这样需要建立的文件已经建立完成,下面是编写代码了。
在Form1.cs文件中添加crystalReportViewer控件,用来显示水晶报表的;再添加“生成报表”按钮,代码全部如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace WinApp


{

/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form

{
private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
private System.Windows.Forms.Button button1;

/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()

{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}


/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )

{
if( disposing )

{
if (components != null)

{
components.Dispose();
}
}
base.Dispose( disposing );
}


Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码

/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()

{
this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// crystalReportViewer1
//
this.crystalReportViewer1.ActiveViewIndex = -1;
this.crystalReportViewer1.DisplayGroupTree = false;
this.crystalReportViewer1.Location = new System.Drawing.Point(0, 40);
this.crystalReportViewer1.Name = "crystalReportViewer1";
this.crystalReportViewer1.ReportSource = null;
this.crystalReportViewer1.Size = new System.Drawing.Size(736, 504);
this.crystalReportViewer1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(280, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "生成报表";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(736, 541);
this.Controls.Add(this.button1);
this.Controls.Add(this.crystalReportViewer1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion


/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()

{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)

{
CrystalReportTest cr1 = new CrystalReportTest();
cr1.SetDataSource(GetDs());

this.crystalReportViewer1.ReportSource=cr1;
}

//该函数返回DataSetTest数据集中表的数据格式的数据集合
public DataSetTest GetDs()

{
//创建DataSetTest数据集对象
DataSetTest ds = new DataSetTest();
string strSql = "select * from test";
string strConnString = "server=.;database=test;user id=sa;pwd=sa;";

SqlConnection conn = new SqlConnection(strConnString);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(strSql,conn);

da.Fill(ds,"test"); //填充数据集,一定要指定表名称:test
conn.Close();
return ds;
}
}
}

网上还有一篇李洪根写的,地址如下:
http://www.microsoft.com/china/community/Column/26.mspx
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现