• RDLC 报表参数、常量及常用表达式的使用方法(上)


      . ASP.NET 程序为RDLC报表提供参数 

       在我们的报表中,往往需要从ASP.NET程序提供一些参数显示在报表的指定位置,第一篇中我们已经讲过怎样传递一个数据集,但是我们只需要一个值,总 不能把这个值放到DataSet中来传输吧(因为DataSet本其实是由XML结成,在传递过程中需要比data本身更多的资源),所以这个时候我们就 需要知道如何传递一个参数到RDLC报表。下面我们就来讲一讲。

      不知道大家还记不记得我在第一篇中的Default.aspx.cs中写的一个Button事件,如下。

    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 * FROM T_BC_LOGS";

        SqlDataAdapter da = new SqlDataAdapter(command);

        da.Fill(ds.T_BC_LOGS);

        reportDataSource.Add(new ReportDataSource("RportDataSet_T_BC_LOGS", ds.T_BC_LOGS));

        //TemplateFiles

        templatePath = "ReportTemplate/LogReport.rdlc";

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

        ////Generate Report

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

    }

      其中我定义了一个泛型变量,如下:

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

      但是我并没有给这个变量赋任何值,我要告诉大家的就是这里变量就是为我们传递参数提供的。好,下面我们就来讲一下怎么传递这个参数。

       1. 首先我们打开ReportTemplate文件夹中的RDLC报表模板,点击报表的编辑区,然后打开Report菜单下的Report Parameters…,在Report Parameters窗口中,我们点击Add 按钮,接着我们输入这个参数的名称与类型(这个名称你要记住,因为下面的编程中要用到),如果这个参数提供的时候不一定每次都有值,那么我们则要选中 All null value的复选框,如下图所示,如果你有多个参数则以此方法添加即可。

     

       2. 我们从Visual Studio IDE左边的ToolBox中拖一个TextBox到这个报表模板的页首(在我的报表中,复杂的表头大部分都是通过TextBox来实现的,可以设计 TextBox的边框及字体等属性来设计,所以后面的内容中我不会再提如何设计复杂的报表页首),右击这个TextBox,选择Expression…, 在Expression窗口中,我们选择Category中的Parameters,然后双击最右边你刚刚加的那个参数,点击确定即可。至此,对报表的设 计完成了。

      3. 修改Default.aspx页面中的程序如下所示:

    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 * FROM T_BC_LOGS";

        SqlDataAdapter da = new SqlDataAdapter(command);

        da.Fill(ds.T_BC_LOGS);

        reportDataSource.Add(new ReportDataSource("RportDataSet_T_BC_LOGS", ds.T_BC_LOGS));

        //TemplateFiles

        templatePath = "ReportTemplate/LogReport.rdlc";

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

        //为参数Parameter1传递This is a parameter

        parameterList.Add(new ReportParameter("Parameter1", "This is a parameter"));

        ////Generate Report

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

    }

    摘录自百度空间~#[懂鍀べ紾悕]#~,在此表示感谢!

  • 相关阅读:
    SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>
    hibernate用注解(annotation)配置sequence
    SSH(Struts2+Spring+Hibernate)框架搭建流程
    spring框架学习(一)
    struts2 s:if标签以及 #,%{},%{#}的使用方法
    js页面跳转参考代码大全
    Struts 2常用的Ajax标签
    Struts2文件上传(基于表单的文件上传)
    struts.custom.i18n.resources国际化
    Struts2常用标签
  • 原文地址:https://www.cnblogs.com/gaolijun1986/p/2152894.html
Copyright © 2020-2023  润新知