RDLC-ReportView控件动态绑定报表和数据源

来源:互联网 发布:linux rpm安装路径 编辑:程序博客网 时间:2024/05/16 14:49

通过ReportView设计界面可以很方便的设置需要绑定的报表文件(rdlc文件)和数据源,但在有时需要根据用户操作通过后台程序动态绑定设计好的rdlc报表和数据源。接下来看看如何方便的实现。。

   //动态绑定报表
        private void BindDataTable(string rdlcstr,string tablename,string sourcename,DataTable dt)
        {
            Common_RV.Reset();
            Common_RV.LocalReport.ReportEmbeddedResource = rdlcstr;
            ReportDataSource datasouce = new ReportDataSource();
            BindingSource bind = new BindingSource(this.components);

            //过滤排序
            dt.DefaultView.RowFilter="";
           
            //dt.DefaultView.Sort = "";
            bind.DataMember = tablename;
            bind.DataSource = dt.DefaultView;
            datasouce.Name = sourcename;
            datasouce.Value = bind;
            Common_RV.LocalReport.DataSources.Clear();
            Common_RV.LocalReport.DataSources.Add(datasouce);
            Common_RV.RefreshReport();
        }

   1、使用方式:

 BindDataTable("ReportTest.CCDetailReport.rdlc","CCDetailTable","ReportDataSet_CCDetailTable",ReportDataSet.CCDetailTable);

 

  2、参数说明:

        rdlcstr:报表文件名,要加命名空间的

       tablename:dataset总的table名

      sourcename:这个比较奇怪,我也没搞清楚为什么一定要这么写,只是在它的design文件中是这么做的,格式是:dataset实例名+_+table名称

       dt:table实例,这里传table过来主要是要使用table的defaultview,可以实现报表的过滤和排序,如果不考虑这些,bind.DataSource可以直接设置成dataset实例就可以了