winform下制作第一张水晶报表 (转自weekzero博客.经测试,没有问题.对于从来没有接触过的朋友来说,值得一看.)...

初学水晶报表,一头雾水,都是希望先找个简单的示例型的文章看看,本篇就非常的适合(仅适合接触水晶报表但是一个都没有写过的同仁)。

本示例环境:windows2003、vs.net2003、Sql Server2000、水晶报表为vs.net2003自带的。
示例程序为winform下读取sql server2000数据库中一表,使用水晶报表显示列表,非常简单的一个,下面就开始了。

首先建立数据库,生成表的sql语句如下:

None.gif CREATE TABLE [dbo].[test] (
None.gif [id] [
decimal ]( 18 0 ) IDENTITY ( 1 1 ) NOT NULL ,
None.gif [name] [varchar] (
50 ) COLLATE Chinese_PRC_CI_AS NULL ,
None.gif [card] [varchar] (
50 ) COLLATE Chinese_PRC_CI_AS NULL ,
None.gif [createdate] [datetime] NULL 
None.gif) ON [PRIMARY]
None.gifGO
None.gif
None.gifALTER TABLE [dbo].[test] ADD 
None.gif CONSTRAINT [PK_test_1] PRIMARY KEY  CLUSTERED 
None.gif (
None.gif  [id]
None.gif )  ON [PRIMARY] 
None.gifGO
None.gif

打开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控件,用来显示水晶报表的;再添加“生成报表”按钮,代码全部如下:

None.gif using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
namespace  WinApp
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**//**//// <summary>
InBlock.gif    
/// Form1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
InBlock.gif        
private System.Windows.Forms.Button button1;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Windows 窗体设计器支持所必需的
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        Windows 窗体设计器生成的代码
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
InBlock.gif            
this.button1 = new System.Windows.Forms.Button();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// crystalReportViewer1
InBlock.gif            
// 
InBlock.gif
            this.crystalReportViewer1.ActiveViewIndex = -1;
InBlock.gif            
this.crystalReportViewer1.DisplayGroupTree = false;
InBlock.gif            
this.crystalReportViewer1.Location = new System.Drawing.Point(040);
InBlock.gif            
this.crystalReportViewer1.Name = "crystalReportViewer1";
InBlock.gif            
this.crystalReportViewer1.ReportSource = null;
InBlock.gif            
this.crystalReportViewer1.Size = new System.Drawing.Size(736504);
InBlock.gif            
this.crystalReportViewer1.TabIndex = 0;
InBlock.gif            
// 
InBlock.gif            
// button1
InBlock.gif            
// 
InBlock.gif
            this.button1.Location = new System.Drawing.Point(2808);
InBlock.gif            
this.button1.Name = "button1";
InBlock.gif            
this.button1.TabIndex = 1;
InBlock.gif            
this.button1.Text = "生成报表";
InBlock.gif            
this.button1.Click += new System.EventHandler(this.button1_Click);
InBlock.gif            
// 
InBlock.gif            
// Form1
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(736541);
InBlock.gif            
this.Controls.Add(this.button1);
InBlock.gif            
this.Controls.Add(this.crystalReportViewer1);
InBlock.gif            
this.Name = "Form1";
InBlock.gif            
this.Text = "Form1";
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new Form1());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CrystalReportTest cr1 
= new CrystalReportTest();
InBlock.gif        
InBlock.gif            cr1.SetDataSource(GetDs());
InBlock.gif
InBlock.gif            
this.crystalReportViewer1.ReportSource=cr1;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//该函数返回DataSetTest数据集中表的数据格式的数据集合
InBlock.gif
        public DataSetTest.testDataTable GetDs()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//创建DataSetTest数据集中的表的对象
InBlock.gif
            DataSetTest.testDataTable ds = new WinApp.DataSetTest.testDataTable();    
InBlock.gif            
InBlock.gif            
string strSql = "select * from test";
InBlock.gif            
string strConnString = "server=.;database=test;user id=sa;pwd=sa;";
InBlock.gif
InBlock.gif            SqlConnection conn 
= new SqlConnection(strConnString);
InBlock.gif            conn.Open();
InBlock.gif            SqlDataAdapter da 
= new SqlDataAdapter(strSql,conn);
InBlock.gif
InBlock.gif            da.Fill(ds);    
//填充数据集
InBlock.gif
            conn.Close();
InBlock.gif            
return ds;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

网上还有一篇李洪根写的,地址如下:
http://www.microsoft.com/china/community/Column/26.mspx
写点自己的感受,在报表建立之前,要先建立一个数据集(是不是都这样呢,看了书,书上也是这么做的.建立数据集之后,把表拖到上面就行了.感觉样子就像视图似的.然后在建立报表的时候,默认为报表专家,下一步之后选新建立的这个数据集.这块有一个问题,就是上文中说到的,建立数据集要先生成一下项目,我在这做的时候,后来没有先生成数据集.也没有问题,问了原作者,他说一定要先生成一下,要不这里选择数据集的时候会没有.而且李洪根那篇文章也有提到,呵呵.难道就这我特殊了.总的来说.按照例子做了一下,对报表有了初步的印象.最好知道报表是什么,怎么使用:)当然这个还很初级,不过事事都从初级过来的不是吗?祝大家好运:) )

转载于:https://www.cnblogs.com/pbwf/archive/2006/04/01/364124.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值