RDLC 报表系列(五) RDLC报表分组

本文只代表作者在一定阶段的认识与理解。

写作前提

在我的博客园中我写了关于一些RDLC报表的使用,请参考这里。因为没有时间,所以没有导入到我的个人博客中。在博客园中相关文章如下:

二、本文内容

  1. 加载RDLC报表数据
  2. 实现数据分组
  3. 总结
  4. 代码下载(下载

三、加载RDLC报表数据

我们的示例是要从学生表中,取出200条学生信息,然后加载到RDLC报表中。

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Data.SqlClient;

using System.Collections.Generic;

using System.Xml.Linq;

using Microsoft.Reporting.WinForms;

using RCLC.DataEntity;



namespace RCLC

{

    public partial class _Default : CustomPageBase

    {

        protected void Page_Load(object sender, EventArgs e)

        {



        }



        protected void ButtonReportGenerate_Click(object sender, EventArgs e)

        {

            List<ReportDataSource> reportDataSource = new List<ReportDataSource>();

            RportDataSet ds = new RportDataSet();

            string templatePath = string.Empty;

            string totalRecords = string.Empty;



            //获得数据

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LoggingConnectionString"].ConnectionString);

            SqlCommand command = conn.CreateCommand();

            command.CommandType = CommandType.Text;

            command.CommandText = "SELECT TOP 200 * FROM T_STUDENT";

            SqlDataAdapter da = new SqlDataAdapter(command);

            da.Fill(ds.T_STUDENT);



            //指定报表模板

            templatePath = "ReportTemplate/StudentReport.rdlc";



            //把获取的数据集合提供给在报表中名为RportDataSet_T_STUDENT数据集

            reportDataSource.Add(new ReportDataSource("RportDataSet_T_STUDENT", ds.T_STUDENT));

            List<ReportParameter> parameterList = new List<ReportParameter>();

            ////Generate Report, 报表可以生成PDF,EXCEL及以其它形式,根据需求去设置

            GetReportMultipleDataSourceFile(reportDataSource, templatePath, parameterList, "pdf");

        }

    }

}

上面的代码已经获取到200条学生的信息,然后把数据从数据库中取出然后加载到 StudentReport.rdlc报表中,下面要做的事情就是对数据进行分组了。

四、实现分组

下面我们根据学生的性别进行分组,步骤如下:

  • 打开RDLC模板文件,点击报表的行标签,然后点击“插入分组(Insert Group)”

  • 在插入分组的窗口中,我们选择需要分组字段(这里的学生的性别),如下图所示

  • 插入分组后,我们可以为分组加一标题,标题就是性别

这样我们就已经分组结束了,下面我们要做的就是看看如果如何。

你也可以从这里下载PDF结果(下载)。

五、总结

通过上面的示例我们学会了如何使用RDLC分组功能实现功能,通过上面的示例我们插入了一个性别的分组,当然也可以分组中再插入子分组,以实现二级、三级或者N级分组。

当然可以对分组进行统计等一系列功能。

你可能感兴趣的:(报表)