在“解决方案资源管理器”中添加新项

    ●选择“数据集”

    ●数据集名称可以任意

    ●在弹出的“TableAdapter配置向导”中新建数据库连接

    ●选择“下一步”直到出现“选择命令类型”对话框

    ●选择“使用SQL语句”

    ●输入SQL语句“SELECT * FROM Tbl1”

    ●单击“完成”,不是“下一步”(这个时候要保存一下项目)

    ●在“解决方案资源管理器”中添加新项

    ●选择“Crystal 报表”

    ●报表的命名应该能体现出是什么表

    ●双击新添加的报表,打开报表编辑器

    ●右键单击报表空白处,选择“数据库”-“数据库专家”

    ●左边选择“项目数据”-“ADO.NET数据集”-前面添加的数据集-表名字

    ●在右侧字段管理器中打开“数据库字段”

    ●将字段拖放到“详细资料”处

    ●如果需要修改自动产生的列名(页眉处出现的字段),右键单击,最上面两个选项可以进行修改调整

    ●右键单击“报表页脚”,“插入”-“汇总”

    ●选择需要进行汇总的字段

    ●选择汇总方式(这里选择的是求和)

    ●如果需要添加文本,右键单击任意空白处,“插入”-“文本对象”

    ●删除前面添加的数据集文件(已经不需要了)

    ●将一个“CrystalReportViewer”控件拖放到页面上

    ●将“RptObject”这个文件添加到“App_Code”文件夹

    using System;
    using System.ComponentModel;
    using CrystalDecisions.Shared;
    using CrystalDecisions.ReportSource;
    using CrystalDecisions.CrystalReports.Engine;


    public class RptObject : ReportClass
    {
        string rptName;

        public RptObject(String rptName)
        {
            this.rptName = rptName;
        }

        public override string ResourceName
        {
            get
            {
                return rptName+".rpt";
            }
            set
            {

            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
        public CrystalDecisions.CrystalReports.Engine.Section Section1
        {
            get
            {
                return this.ReportDefinition.Sections[0];
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
        public CrystalDecisions.CrystalReports.Engine.Section Section2
        {
            get
            {
                return this.ReportDefinition.Sections[1];
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
        public CrystalDecisions.CrystalReports.Engine.Section Section3
        {
            get
            {
                return this.ReportDefinition.Sections[2];
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
        public CrystalDecisions.CrystalReports.Engine.Section Section4
        {
            get
            {
                return this.ReportDefinition.Sections[3];
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
        public CrystalDecisions.CrystalReports.Engine.Section Section5
        {
            get
            {
                return this.ReportDefinition.Sections[4];
            }
        }
    }

    窗体部分:
    private DataSet GetDataSet()
    {
        System.Data.SqlClient.SqlConnection mySqlConnection = new System.Data.SqlClient.SqlConnection("
server = localhost;database = RptTest;uid = smartkernel;pwd = 123");
        System.Data.SqlClient.SqlCommand mySelectSqlCommand = mySqlConnection.CreateCommand();
        mySelectSqlCommand.CommandText = "SELECT [Name],[SellCount] FROM Tbl2";//直接在这里选择要显示的列就可以了
        System.Data.SqlClient.SqlDataAdapter mySqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
        mySqlDataAdapter.SelectCommand = mySelectSqlCommand;
        DataSet myDataSet = new DataSet();
        mySqlDataAdapter.Fill(myDataSet);
        return myDataSet;
    }

    RptObject aRptTbl2 = new RptObject("RptTbl2");//新建报表对象

    protected void Page_Load(object sender, EventArgs e)
    {
        aRptTbl2.SetDataSource(GetDataSet().Tables[0]);//将表对象作为数据源
        this.CrystalReportViewer1.ReportSource = aRptTbl2;
    }

    protected void Page_Unload(object sender, EventArgs e)
    {
        aRptTbl2.Dispose();
    }


该文章转载自网络大本营:http://www.xrss.cn/Dev/DotNet/200772815227.Html

posted on 2007-12-07 01:45  小咪  阅读(716)  评论(1编辑  收藏  举报